Variables in PhP

0
59

In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instructions that tell the computer what to do and data that the program uses when it is running. In Other words, variable is nothing it is the just name of the memory location. It is simply a container i.e used to store both numeric and non-numeric information.

Rules for Variable declaration in PhP

  • Variables in PHP starts with a dollar ($) sign, followed by the name of the variable.
  • The variable name must begin with a letter or the underscore character.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain space

Assigning Values to Variables

Assigning a value to a variable in PHP is quite east: use the equality (=) symbol, which also to the PHP’s assignment operators. This assigned value on the right side of the equation to the variable on the left. A variable is created the moment you assign a value to it:

Example

<?php
  $myWebsite = "Study Warehouse";
  echo $myWebsite;
?>

Output: Study Warehouse

In the above example create a variable ($myWebsite) containing a string with value = ”Study Warehouse”. To print the Website Name pass $myWebsite inside echo statement.

Another example of PhP variable by using concatenation (String)

<?php 
  $myWebsite = "Study Warehouse";
  echo $myWebsite." is an online platform to Study online";
?>

Output: Study Warehouse is an online platform to Study online

In the above example Variable ($myWebsite) hold value = ”Study Warehouse”. To concatenate this with a string (“is an online platform to Study online”) use dot(.) between variable name and string.

Another example of PhP variable by using concatenation (Sum of two numbersString)

<?php
$first = 100;
$second = 200;
$third = $first + $second;  
echo "Sum = ".$third;
?>

Output: Sum = 300

In the above example Declare $first , $second variable with value = 100, 200 respectively.
Now add these two numbers using arithmetic operator (“+”). Sum of these two variable result stored in a third variable ($sum). Now print the sum passing ($third) with echo statement with a string.

Destroying PHP Variables

To destroy a variable, pass the variable to PHP’s unset( ) function.

Example

<?php
$name="Pardeep Patel";
echo $name;
//unset( ) function destroy the variable reference.
unset($name); 
//after delete the variable call it again to test
?>

Output:

Pardeep Patel

Notice error undefined third variable

In the above example declare variable $name hold value = ”Pardeep Patel”. In this program we used unset() function to delete a particular variable. First it show the output: “Pardeep Patel”, because we pass unset function after echo statement. Now pass variable name inside unset($name) function output will show an Notice error(Variable is undefined).

Note: Trying to access or use a variable that’s been unset( ), as in the preceding script, will result in a PHP “undefined variable” error message. This message may or may not be visible in the output page, depending on how your PHP error reporting level is configured.

Variable names in PHP are case-sensitive

<?php
$name="Pardeep";
$NAME="Patel";
echo $name;
?>

Output: Pardeep

Variable names in PHP are case-sensitive. As a result, $name refers to a different variable than does $NAME.

Inspecting Variable Contents (Variable Property)

PHP offers the var_dump( ) function, which accepts a variable and X-rays it for you.

Example

<?php 
//define variables
$website = "StudyWarehouse";
$number=30;
//display variable contents
var_dump ($website);
var_dump($number);
?>

Output:

string ‘StudyWarehouse’ (length=14)

int 30

In the above example we use var_dump( ) function to check the Contents (property) of variable, $website hold a string value =”StudyWarehouse” while $number hold an integer value = 30. Now pass this variable inside var_dump($website, $number) function . It show all information related this (data type of variable, length (only string), value).

LEAVE A REPLY

Please enter your comment!
Please enter your name here