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 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.
Here is an example of how to write single and multiple line comments within your code
<?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.
Now we will look at Variables and data types in PHP.
Variables
In general programming a variable is a location with a name that can be used to store a value. A value of the variable can be changed during the execution of the script. That’s why it is called a variable. In PHP, a variable is represented by a dollar sign followed by the name of the variable. Most importantly the variable name is case sensitive. Variable names should start with a letter or underscore. The rest of the name can contain any number of letters, numbers, or underscores.
example of a variable
<?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
In general programming languages, you need to specify what is the data type of a variable. But in PHP according to the value you assign to a variable it will determine its data type.
example
<?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
Constants
A constant is an identifier for a simple value. When it comes to a constant, that value cannot change during the execution of the script. The naming rule applies the same as the rules of variables. It is case-sensitive. By convention, constant identifiers are always uppercase.
<?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