Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
PHP

by Nazly Ahmed
Welcome back to this month's article on PHP. In our previous articles we looked at the basics and how we can embed PHP code into HTML and the other way. This month we will go further deep into code.
Like I said last month you will need to use a text editor like notepad or gedit to type the sample code I have given in these examples. Then save the file giving a file name of your choice but make sure it ends with a .php extension. You need to save the files under the http docs directory according to your Web Server. To test the script, simply call the local URL of the php script which in most cases will be http://localhost/youfilename.php
You can check this list of editors http://en.wikipedia.org/wiki/List_of_PHP_editors which you could use to code PHP.
Before starting on today's topic I will explain a bit about using Comments in your code.
Comments
Comments are very important to describe what you have written in your code. When you are writing your code you will need to separate your comments from code so that PHP won't parse the comments thinking it as code. If it does it will lead to syntax errors.
<?php echo "Hello World"; #Single Line Comment echo "Another Hello World"; //Single Line Comment echo "Yet Another Hello World"; /* This is a multiple line comment */ ?>
As you can see in the above example if you use # or // at the start of the line that line would be considers as a comment. If you place multiple lines between /* and */ the entire block within these characters will be considered as comments.
Variables
<?php $a = "Hello world"; echo $a; //Prints Hello World on the browser ?>
In this code $a is a valid variable. The value "Hello world" will be assigned to the variable $a. The echo command will print the content of the variable $a. This is the same Hello World example we did last month but here we are using variables.
We will further see valid and invalid variable names. Invalid variable names will result in syntax errors.
<?php $foo = "Test 1"; //Valid $_foo = "Test 2"; //Valid $_3foo = "Test 3"; //Valid $6foo = "Test 4"; //Invalid$fo-o = "Test 5"; //Invalid
?>
Data Types
<?php $a = 1; //$a will be a integer type variable $b = "Hello"; //$b will be a string type variable $c = 1.5; //$c will be a float type variable $d = true; //$d will be a boolean type variable ?>
In our next month’s article we will look more in towards
<?php // Valid constant names define("FOO", "Hello"); define("PI", 3.14159265); define("NUM", 1); // Invalid constant names define("2FOO", "something"); ?>
That’s it for this month. In coming months we will look further deep into the PHP language. Until then Happy Coding!!
Post new comment