Php Variables

Php Variables


Variables are used for storing information or values

The main way to store information in the middle of a PHP program is by using a variable

The most important things to know about variables in PHP.

  • All variables in PHP have a leading dollar sign ($).

Example : $name;

  • Variables are assigned with = operator, assigning the variable name on the left hand side to the value on the right hand side.

Example : $num = 4;

  • Php is a loosely typed language and do not needs be declare before assignment
  • Php automatically takes the variable type according to the assigned value.

Example : $num = 10; will be taken automatically as interger

  • Variables used before they are assigned have default values.
  • PHP does a good job of automatically converting types from one to another when necessary.
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($name and $NAME are two different variables)

PHP has a total of eight data types which are used to construct a variables:

  • Integers:are whole numbers, without a decimal point, like 2018.
  • Doubles:are floating-point numbers, like 3.11111 or 32.3.
  • Booleans:have only two possible values either true or false.
  • NULL:is a special type that only has one value: NULL.
  • Strings:are sequences of characters
  • Arrays:are named and multiple values having same datatype.
  • Objects:are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
  • Resources:are special variables that hold references to resources external to php

Integers


The whole numbers, without a decimal point, like 12345. Integers can be assigned to variables, or they can be used in expressions, like so:

$int1 = 12345;

$int2 = 2 + 5;


Doubles


It has decimal 1.23456 or 32.2.

code:

<?php

$many = 1.1111111;

$many_2 = 2.2222222;

?>


 

Boolean


They have only two possible values either true or false. PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE, which can be used like so:

if (TRUE)

print(“if true print this statement<br>”);

else

print(“if false then print this statement<br>”);


NULL


NULL has only has one value

$var = NULL;


Strings


They are sequences of characters, like “PHP supports string operations”. Following are valid examples of string

$string_1 = “This is a string in double quotes”;


Variable Scope


Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types:

  • Local variables
  • Global variables
  • Static variables

 

Global and Local Scope


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

Example

<?php
$xyz = 15; // global scope

function function_name() {

echo “<p>Variable x inside function is: $x</p>”;
}
function_name ();

echo “<p>Variable x outside function is: $x</p>”;

// if we are displaying variable x inside function it will give error
?>


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

Example

<?php
function function_name() {
$xyz = 15; // local scope
echo “<p>Variable x inside function is: $x</p>”;
}
function_name();

// using x outside the function will generate an error
echo “<p>Variable x outside function is: $x</p>”;
?>


PHP The static Keyword


When function is fully executed it’s variable all gets deleted and sometime we don’t want the variable values to get deleted and we want them to hold those values.

To do this, use the static keyword when you first declare the variable:

Example

<?php
function function_name() {
static $xyz = 0;
echo $xyz;
$xyz++;
}

function_name();
function_name();
function_name();
?>

 

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *