Variables: Variables are names which helps to store some value inside it.Variable can holds single value at a time. The main difference between, array and variable is, an array can hold multiple values/elements at a time, while a variable can hold single value at a time.

File – var1.php

<?php
$a=5;
echo $a;
echo ‘<br>’;
$a=’chandragupt’;
echo $a;
?>
output

Let’s see some variables operations…

File – code1.php

<?php
// variables ‘a’ & ‘b’ has contains their own values
$a=10;$b=20;
// addition of variables ‘a’ & ‘b’ stored into variable ‘c’
$c=$a+$b;
// displays output of variable ‘c’
echo $c;
?>
output

File – code2.php

<?php
// variable ‘name’ is a string variable right-now
$name=’microcodes.in’;
echo ‘website is: ‘.$name;
$name=’https://www.microcodes.in’;
echo ‘<br>website now: ‘.$name;
?>
output

File – code3.php

<?php
$a=30;$b=20;
// we can directly print values using statement ‘echo’
echo ‘addition is: ‘;
echo $a+$b;
echo ‘<br>subtraction is: ‘;
echo $a-$b;
echo ‘<br>multiply is: ‘;
echo $a*$b;
echo ‘<br>division is: ‘;
echo $a/$b;
?>
output

In PHP, we can create variables in many way…

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • 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 ($age and $AGE are two different variables)
  • PHP is a loosely typed language?

Because PHP changes it’s datatype according to its values during program runtime. We don’t need to define the data-type in advance in PHP script.In above, we have shown this example on variables.

  • Variables datatypes

There are some following datatypes present in PHP. Ex. Integer, string, Boolean, double, array, NULLetc.

File – code4.php

<?php
// gettype() function helps to show datatype of a variable
$a=90;
echo gettype($a);
echo ‘<br>’;
$a=’computer’;
echo gettype($a);
echo ‘<br>’;
$a=90.6;
echo gettype($a);
echo ‘<br>’;
$a=true;
echo gettype($a);
echo ‘<br>’;
$a=[1,2];
echo gettype($a);
echo ‘<br>’;
$a=NULL;
echo gettype($a);
echo ‘<br>’;
?>
output

  • typecasting

PHP supports implicit typecasting – I mean, int -> string, int -> float, float -> int etc.

Let’s see the usages below…

To cast a value to an integer, you use the (int) type casting operator.

The (int) operator casts a float to an integer. It’ll round the result towards zero. For example:

File- typecast1.php

<?php
echo (int)12.5 . ‘<br>’; // 12
echo (int)12.1 . ‘<br>’; // 12
?>
output

  1. We can typecast an integer to a float.

File – typecast2.php

<?php
echo (float)500; // 500
echo ‘<br>’;
echo (float)12; // 12
?>
output

2. We can typecast integer to string.

File-typecast3.php

<?php
$a=200;
echo gettype($a);
echo ‘<br>’;
$a=(string)$a;
echo $a;
echo ‘<br>’;
echo gettype($a);
?>
output

Same like, we can also typecast string to integer.

File – typecast4.php

<?php
$a=’200′;
echo gettype($a);
echo ‘<br>’;
$a=(int)$a;
echo $a;
echo ‘<br>’;
echo gettype($a);
?>
output