Home > Courses > Web development using PHP and MySQL > PHP Variables and Comments

PHP Variables and Comments

Subject: Web development using PHP and MySQL
PHP Variables
PHP are containers or name for holding information. in PHP a variable name must start with a dollar sign $

Example 1
$name="Ben Onuorah";


Variable Naming Rule
1. Variable name must start with a letter or the underscore but NOT a number.
2. Variable name can only contain alpha-numeric characters and underscores (A to z, 0 to 9, and _ )
3. Variable names are case-sensitive in PHP, for example $msg is not the same as $MSG

PHP is loosely typed language, that is, it determine the data type of a variable at run time when you request the PHP page or function. Languages such as C, C++, Java and C# are strongly typed as they demand that you declare every variable with it's data type (integer, float or string) before usage.

Example 2

<?php
//Variables Declaration
$name = "Ben Onuorah";
$age = 35;
$height = 5.6;

//Variables Output
print "$name is $age years old, and he is $height";
?>


Example 3

<?php
$x = 100;
$y = 250;
echo "The addition is ".$x + $y;
?>


Note: the period (.) in between the echo (output statement) is called concatenation, it is use to join “plain text” to a $variable when doing output.

Special Variables
PHP also have some special types of variables for different types of operations:

$_POST: holds variables provided through the form POST method
$_GET: holds variables provided through the GET method
$_REQUEST: is a PHP super global variable which is used to collect data with POST or GET method
$_SESSION: holds any variables that are currently registered in a session
$_SERVER: holds server related information such as headers and file paths
$_FILES: holds any variables provided through file uploads.
$_ENV: handles server environment related variables.

PHP Comments
Comment is a line that is not executed as a part of the program. Its purpose is to explain what a line or block of code is doing. For easy maintenance of the code.

Single line comment
Single line comment is done using double backward slashes //

Example 4

<?php
//declare two variables
$x = 100;
$y = 250;
//add both variables and store them into another variable
$sum = $x+$y;
//display the output or result
echo "The sum of ".$x." and ".$y." addition is ".$sum;
?>


Multiple line comment
Multiline comment start with /* and end with */

Example 5

<?php
/* this program add the two scores
and display the result */
$x = 100;
$y = 250;
echo "The addition is ".$x + $y;
?>


By: Benjamin Onuorah

Comments

No Comment yet!

Login to comment or ask question on this topic


Previous Topic Next Topic

Supported by