PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables.
  • Variable starts with $ sign.
  • Variable name must start with a letter or the underscore character.
  • Variable name can contain alpha-numeric character (A-Z. 0-9).
  • Variable name are case-sensitive ($name and $NAME are two different variables).
Example
<?php
$name = "RasmusLerdorf";
echo "PHP Father:  $name";
?>
Output PHP Father: RasmusLerdorf In PHP, there are three variables:
  • local variable
  • global variable
  • static variable
Local variable PHP local variable is declared inside a function. It has local scope and can be accessed within the function. Example:
<?php
function fun() {
    $x = 10; // local scope
echo "<p> x variable inside of the function: $x</p>";
}
fun();
// Generate an error because x is outside the function
echo "<p> x variable outside of the function: $x</p>";
?>
Output x variable inside of the function: 10 Notice: Undefined variable: x in C:\xampp\htdocs\crud\HELLO.PHP on line 8 x variable outside of the function:  Global Variable The Global is a keyword that is used to access global variable within a function. Example:
<?php
$x = 10; // global variable
$y = 5;  // global variable
$z=5; // global variable
function fun() {
global $x, $y,$z;
    $y = $x * $y+$z;
}
fun();
echo $y; // outputs 55
?>
Output 55 Static variable Php static is a keyword that is used to create a static variable. Static variable is not be deleted when a function is completed /executed. Example
<?php
function fun() {
static $val = 0;
echo $val;
    $val++;
}
fun();
echo "<br>";
fun();      // call function
echo "<br>";
fun();      // call function
echo "<br>";
?>
Output 0 1 2

Related Topics

PHP var_dump() Function

PHP var_dump() Function The var_dump() function in PHP is used to dump the information about a variable. It displays the structured information (arrays and objects) about one or more expressions. This function is also effective...

1 minute read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

Python main() function

What is a main() function? In most of the programming languages, there is this one special function i.e., main() which is executed firstly everytime when we run the program. The special...

6 minutes read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

1 minute read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP Explode() Function

PHP Explode The PHP explode( ) is used to break a string into an array. The PHP explode( ) allow us to break a string into a smaller text with each break...

1 minute read.

PHP print() Function

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP is_object() Function

The is_ object() function in PHP finds whether the specified variable is an object or not. It returns a Boolean value TRUE if the specified var is an object else it returns FALSE. Syntax is_object(mixed$var) var(required)- This parameter represents the...

1 minute read.

PHP chop() Function

PHP chop() Function The chop() function in PHP removes whitespaces or other predefined characters from the right end of the specified string. Syntax chop(str,charlist) Parameter str(required)- This parameter specifies the string to check. charlist(optional)- This parameter...

1 minute read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

1 minute read.

How to use Date and Time in PHP

HOW TO USE DATE AND TIME IN PHP: PHP time and date function  informs us about current date and time. it can be manipulated also. How time() function works: The time() function...

2 minutes read.

PHP str_getcsv() Function

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP sprintf() Function

PHP sprintf() Function The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function. The related functions are...

8 minutes read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

PHP strtolower() Function

The strtolower() function in PHP is used to convert a string to lowercase. This function is binary-safe. The related functions are as follows: strtoupper()lcfirst()ucfirst()ucwords() Syntax strtolower ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP addslashes() Function

PHP addslashes() Function The addslashed() function in PHP is used to return a string with backslashes in front of predefined characters. This function is only used with some characters which are...

1 minute read.