PHP: Variables are case sensitive

This entry is part 6 of 54 in the series PHP Tutorial

In PHP variables declared are case sensitive.

<?php
$myText = 'I love programming ';
$mytext = 'the most liberal programming language in the world PHP.';

echo "$myText  $mytext";
?>
Try The Code

$myText is not the same as $mytext in PHP.

Note also variable string can be declared in single quote such as ‘I love programming’. We have put 2 variables in the echo statement.

If you changed echo statement into using single quotes, it will not work in this case.  Try to stick to using double quotes in echo statement.

PHP: Variable scope

This entry is part 7 of 54 in the series PHP Tutorial

The scope of a variable is the context within which it is defined.

Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:

<?php
$a = 1; /* global scope */ 

function test()
{ 
    echo $a; /* reference to local scope variable */ 
} 

test();
?>

If you type the code into a good IDE such as phpStorm, you will receive a complain from the editor saying that $a is an undefined variable.

The above script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope.

You may notice that this is different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition.

PHP: Static variable

This entry is part 8 of 54 in the series PHP Tutorial

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

Consider the following example:

<?php
function final_no()
{
    static $num = 0;
    echo $num;
    $num++;
    echo "<br>";
}

final_no();
final_no();
final_no();

?>
Try The Code

The output from the above code is 0, 1, 2.

The $num was declared once as a static variable and will be initialized on the first call to final_no().  Everytime final_no() is called, $num will not be re-initialized to 0.

PHP: Constants

This entry is part 9 of 54 in the series PHP Tutorial

The syntax for defining constant is as below:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ])

<?php

define("hello", "Hi There.");
echo hello; 
echo "<br>";
echo Hello; 
echo "<br>";

//To allow case insensitive, set the third parameter to be true.
define("greeting", "How are you?", true);
echo GREETING; 
echo "<br>";
echo Greeting;

?>
Try The Code

Note that there are 2 parts in the above code.  The top part is case sensitive and the second part is case insensitive.

You can try to run the code here.

PHP: Variable with global keyword

This entry is part 10 of 54 in the series PHP Tutorial

In PHP, global variables must be declared global inside a function if they are going to be used in that function.

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?>

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version.