Beginners’ guide to PHP, part -1

No Gravatar

Before going to the serious topic, let me put some worlds on why to choose php to learn.

In a web page, we can have lots of static element like image, text etc. But if we want something dynamic, we need some dynamic programming. For dynamic web programming we need to know some programming language. Java EE is the best for dynamic web programming as far as I know. But now a days php is highly used as a dynamic web programming language. Because php is easy to learn and easy to manipulate.  It is faster to code and faster to execute. Its development complexity is less then Java EE. But again there is a question, why we choose php over ASP, JSP, Perl, CGI, or Cold Fusion. When we make a decision that is also based on the business requirements of the application. Simply as php holds low development complexity and it has cross platform computation ability, we choose it. PHP is very easy. Syntax of PHP is based on C.  Okay enough said, as you are here, I can guess, you have the interest of learning php. So lets go for that.

Before starting, we need set up our development environment. We need some tools. Basically we need only a IDE and a server for now. so get them installed in your machine.

eclipse     pdt- click here to download

xampp- click here to download

how to install them:

for windows user, you will find xampp are self extracting. Simply double click on xampp.exe file and follow the instruction.

and eclipse is portable. no need to install. simply a click makes it start.

for linux user: keep your xampp downlodead tar.gz file in you desktop.

Open terminal (Ctrl+Alt+T) and go to desktop.

cd Desktop

Extract the downloaded archive file to /opt:

$ sudo tar xvfz xampp-linux-1.7.3a.tar.gz -C /opt

To start XAMPP, just type in terminal

$ sudo /opt/lampp/lampp start

You will see something like below in your screen

Starting XAMPP 1.7.3a…

LAMPP: Starting Apache…

LAMPP: Starting MySQL…

LAMPP started.

To stop it just type

$ /opt/lampp/lampp stop

now make a test, that is it working or not?

go to your xampp directory. find htdocs.

if you are windows user, stay calm. for linux user

open your terminal again. excute following command.

~$ cd /opt/lampp

~$ sudo chmod -R 777 htdocs

now its ready to work.

start your xampp. for windows user, just go to your all programs and finod xampp and click on start buttons and linux user, excute following commands:

$ sudo /opt/lampp/lampp start

now go to htdocs derectory again.  and open a new file named something helloword.php.

now open it and write there following codes:

<?php

echo ‘Hello world’;

?>

now save it and open your favorite browser. I use Mozilla FireFox, you may use google chrome. even you can use Internet explorer though I don’t know why people use it still now.

Now write on address bar . http://localhost/helloword.php

what happened? did you find anything in your browser. if you find ‘Hello world’ written in your browser, its working nicely.

now give thanks to God as everything is ready to go froward .

our next topic on data type

data type :

basic syntax: php file should be  contain .php extension and  php program  should start with <?php and end with with ?>

for example :

<?php

if($true){

echo "true";

}else {

echo "false";

}

?>

every programming language have some data types and its true for php also.

PHP has eight different type data type .First four are scalar, they are  Boolean, integer, floating point and stirng.  Next tow are compound types : they are array and object . and lastly  php has two spacial data types.  they are NULL and resource .

we first discuss our scalar dataytpe here . we will discusses rest of datatype later.

and interesting things is that php is loosely-typed language and so you don’t need to declare a variable with a specific type as python.

All variable in php denoted with a leading dollar sign ($).

example -

$my_name = “rokon”;

$my_cell_no = 1234569789;

$a_floating_point_number = 4.00043;

here, I already shown you how to assign a value. but one things to remind..

$num = “3+0.123″;

and

$num = 3+0.123;

is not same.

double quote makes the first one as string. So If we print, it will print 3+0.123, but second one will print 3.123.

and php support auto type casting . example-

<?php

$num1 = 100; // integer

$num2= 3.0991;    // floating point

$num3 = $num1 +$num2;

echo “$num3″;

?>

it will print 103.0991.

Scope of a variable is an important issue in programming. Any PHP variable not inside in function are global that means, If you declare a variable in the top of your php file and assign it, it will remain same rest of the file unless its reassigned.

Booleans

The boolean types named after mathematician George Boole . It is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

syntax: To specify a boolean literal, use the keywords TRUE or FALSE. Both are case-insensitive.

example:

<?php

$foo = True;   // assign the value TRUE to $foo

?>

Integers

Integers are whole numbers. The range of  integers in  PHP is equivalent to the range of the so-called long data type of the C language .Typically, this means the range from -2147483648 to +2147483647 on a 32 bit architecture, but may vary depending on your platform.

PHP allows you to write integers in three ways:  decimal, octal, and hexadecimal.

example:

<?php

$a = 1234;     // decimal number

$a = -123;     // a negative number

$a = 0123;     // octal number (equivalent to 83 decimal)

$a = 0x1A;     // hexadecimal number (equivalent to 26 decimal)

?>

Floating point numbers

Floating point numbers (also known as “floats“, “doubles“, or “real numbers“) can be specified using any of the following syntaxes:

<?php

$a = 1.234;

$b = 1.2e3;

$c = 7E-10;

?>

Strings

Web applications usually move text around more often than they make complex mathematical calculation. String represent a serious of characters of limited length and can contain any kind of data, including binary data.  You can write a string value by single-    quote(‘) or double-quote(“). Whichever you choose, the opening quote character must match the closing quote character.

If the string is enclosed in double-quotes (“), PHP will interpret more escape sequences for special characters:

Sequence Meaning
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0×09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\” double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation

example :

<?php

echo 'this is a simple string.';

$myName = "rokon";

echo " My name is " . $myName;

?>

References:

1. http://www.php.net/manual/en/index.php

2. Core PHP programming by LEON ATKINSON

Part two will be available soon, stay tuned.


Bazlur RahmanAbout the Author: A. N. M. Bazlur Rahman is the editor of Vanquish Tech’s blog. He is a student of Institute of Information Technology of University of Dhaka of its software engineering program. He is very much interested in software development and working with php right now. He likes Java programming language very much. Through this web site, he wants to make a large community of software development especially web application development. He enjoys his pastime with programming, listening songs, blogging, watching movies and different computer stuff. You can follow him on twitter .

2 Comments

Leave a Reply

Your email is never shared.Required fields are marked *

This site is using OpenAvatar based on