Variables are "containers" for storing information.


Declaring Variables in php:- 


Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.


For Example:-


After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.

Note: When you assign a text value to a variable, put quotes around the value. Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

A variable can have a short name (like i and s) or a more descriptive name (student, sname, roll_number).

Rules for PHP variables:

  • A variable starts with the $ sign
  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number

echo and Print statement:-

The PHP echo and print statement is often used to output data to the screen. The following example will show how to output text and a variable: 


The following example will output the sum of two variables

<?php
   $i = 5;
   $s = 4;
    echo $i + $s;
?>


Variables Scope:-

In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be used. 

PHP has three different variable scopes:
  • local 
  • global 
  • static

Global and Local Scope:-

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. 

<?php
$i = 3# global scope
function myTest() {
   // using i inside this function will generate an error    echo "<p>Variable i inside function is: $i</p>";

myTest();

echo "<p>Variable i outside function is: $i</p>";
?>

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

PHP The global Keyword:-

The global keyword is used to access a global variable from within a function. To do this, use the global keyword before the variables (inside the function)

<?php
$x = 5;
$y = 10;

function myTest() {
    global $x, $y;
    $y = $x + $y;
}

myTest();
echo $y; // outputs 15?>


PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.

The example above can be rewritten like this:-

<?php
$x = 5;
$y = 10;

function myTest() {
    $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];


myTest();
echo $y; // outputs 15?>

The static Keyword:-

Normally, when a function is completed. all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
use the static keyword when you first declare the variable.

<?php
function myphp() {
    static $i = 0;
    echo $i;
    $i++;
}

myphp();
myphp();
myphp();
?>

IF you have any question about this lecture you can comment blow. feel free to contact me.
you can also Visit my YouTube channel by clicking on this Link YouTube Channel.

0 comments:

Post a Comment

Thanks

 
Blogger Templates1 school © 2013. All Rights Reserved. Powered by Blogger
Top