02 January 2011

Php For Beginners (Lesson 2)

Overview

Since you are reading this article therefore I am assuming that you have read my first lesson (If not, I would recommend that you should read lesson 1 first). So I will be continuing from where I left in lesson 1 and in order to give you an idea what you are going to learn in this lesson, I am stating just the names of those

Topics:

• Comments
• Constants
• Variables

Comments

In php there are three ways to write comments. Two of them are used to comment only one line and the third one can be used to comment multiple lines. First method is to simply write “#” in the start of the line that you want to comment, in second method you need to write “//” in the start of the line you need to comment and in the third method you need to encapsulate multiple lines that are to be commented in between “/*” and “*/”. “/*” is used to start the comment and “*/” is used to end the comment.

Example
< ?php
echo “test line”; #This is a one line comment
echo “Another test line”; //This is also a one line comment
/*This is a multi line comment
Another line….. 
Another line…..
End of multi lines comment*/
?>

Constants

As the name suggests, Constants have a fixed value that once set cannot be changed or re-defined. In php if you need to set a constant then you have to use define() function, but remember once a constant is defined, it can never be changed or undefined.

Syntax

define(constant name,constant value,case_insensitive)

Example # 1
< ?php
define(“test_constant”,”Hey There!”);
echo test_constant; // outputs “Hey There!”
echo TEST_CONSTANT; // outputs “TEST_CONSTANT”
?>

In above mentioned example we used only two arguments (name & value) and constants are by default case sensitive so first line will give the write output but the second line will simply display the name of the constant.

Example # 2
< ?php
define(“test_constant”,”Hey There!”,TRUE);
echo test_constant; // outputs “Hey There!”
echo TEST_CONSTANT; // outputs “Hey There!”
?>

In this example as we have set the third argument (case insensitive) to TRUE so this constant is now case insensitive and both lines will output the right content.

Variables

Variables, as their name suggests, are used to store values that can be changed. In php variables names are case sensitive and are represented by a dollar sign followed by the name of the variable. In contrast to constants, variables don’t need define() function to store values, instead variables can be easily assigned values by using “=”.

Example
< ?php
$variable_name=”Test content”;
echo $variable_name; // outputs “Test Content”
$another_variable=85;
echo $another_variable; // outputs 85
?>

Well guys, I guess this is all for the second lesson now you guys should practice these concepts because “Practice Makes A Man Perfect”.

By the way I am deliberately going slow and covering fewer concepts in these lessons so that it will be easier for the beginners to digest them. ;)

References

http://www.php.net
http://www.w3schools.com

next Previous Lesson --- Next Lesson next

No comments: