PHP for Beginners - Variables and Data types

PHP for Beginners - Variables and Data types

Variables

Variable is nothing it is just name of the memory location.

Variable is simply a container.

Rules for Variable declaration

  • 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 easy, use the equality(=) symbol.

This assign 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.

<?php
$city="Chennai";
$_city="Madhurai";
$city3="Tanjore";
?>

PHP 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.

In PHP we have three different variable scopes:

  • Local
  • Global
  • Static

Local 

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

<?php 
function Test() 
{ 
$a = 7;
}
?>

Global

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. The global keyword is used to access a global variable from within a function:

<?php 
$a = 9;  //Global Variable 
function Test() 
{ 
$b = 7;
}
?>

Static

When a function is executed, all of its variables are deleted. But if you want any variable not to be deleted, the static keyword is used when you first declare the variable.

<?php 

function Test() 
{ 
static $b = 7;
$b++;
}

?>

Now that you have learnt about the Different Variables, let’s move ahead with the PHP Tutorial and have a look at the various PHP Data types.


Data types:

A variable can store different types of Data. Let’s have a look at some of the data types supported by PHP.

PHP Data types:

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object
  • NULL

String

A string is a sequence of characters. In PHP, you can write the string inside single or double quotes.

<?php
$city="Chennai";
$_city='tanjore';
var_dump($city); 
var_dump($_city);

?>

Here, PHP var_dump() function returns the data type and value.

Integer

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. An integer must have at least one digit and can be either positive or negative.

?php
$mark=94;
$_mark=-5;
var_dump($mark);
var_dump($_mark);

?>

Float

A float or floating point number is a number with a decimal point or a number in exponential form.

<?php
$mark=94.65;
$_mark=-5.87;
var_dump($mark);
var_dump($_mark);

?>

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE. They are often used in conditional testing.

<?php
$a = 10; 
$b = 20;
$c=$a>$b
var_dump($c);
?>


PHP Object

An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly declared. We need to declare a class of object using the class keyword.

<?php 

class Student 
{ 
function Student() 
{ 
$this->name = “XYZ”; 
} 
}
 
?>

PHP Array

An array stores multiple values in one single variable.

<?php 
$students = array(“Daniel”,”Josh”,”Sam”); 
var_dump($students); 
?>


Try it out yourself:

Step 1: Goto C:/XAMPP/ and run xampp-control.exe to open the Control Panel.

Step 2: Goto C:/XAMPP/htdocs/test then create a file "Variables.php". Inside this file Write the below scripts.

<?php
echo("Examples of Variables <br/>");
$city="Chennai";
$_city="Madhurai";
$city3="Tanjore";
echo("city =$city <br/>");
echo("_city=$_city <br/>");
echo("city3=$_city <br/><br/>");
echo("Examples of Local variables<br/>");
function Test() 
{ 
$a = 7;
echo("Local Variable a=$a<br/><br/>");
}
Test();
echo("Examples of Global variables<br/>");
$b=10;
function Test1() 
{ 
$c = 7;
echo("Local Variable c=$c<br/>");
}
Test1();
echo("Global Variable b=$b <br/><br/>");

echo("Examples of Static variables<br/>");
function Test2() 
{ 
static $a = 7;
echo("Static Variable a=$a<br/>");
$a=$a+1;
}
Test2();
Test2();
?>

Step 3: Open any browser and type https://localhost/test/Variables.php then see the output of the PHP Script.

No alt text provided for this image


Happy Learning !!!...

****************************************************************************************************************************************************

Follow me on

ResearchGate

LinkedIn

Blog


PHP for Beginners

  1. Introduction
  2. How to install PHP?
  3. How to run the PHP Script?
  4. Variables and Data types

要查看或添加评论,请登录

MuthuKumaran Singaravelu的更多文章

社区洞察

其他会员也浏览了