PHP Variables

Variables are used to store data that can be manipulated and retrieved during the execution of a script. Variables in PHP are easy to use, dynamically typed (meaning that you do not need to declare their type explicitly), and essential for creating dynamic, interactive web applications.


Declaring Variables in PHP


  • PHP variables start with a dollar sign ($) followed by the variable name.
  • Variable names must begin with a letter or an underscore (_) but cannot start with a number.
  • Variable names can only contain alphanumeric characters and underscores (A-Z, a-z, 0-9, and _).
  • Variable names are case-sensitive ($variable and $Variable are different).
Example
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$variableName = "Hello, World!";

echo $x;
echo "<br>";
echo $variableName;
?>

</body>
</html>

Try it yourself

In the example above, the variable $x will hold the value 5, and the variable $y will hold the value "Hello, World!".

Note

When you assign a text value to a variable, put quotes around the value.

Outputting Variables


You can display the value of a variable using echo or print.

Example
<!DOCTYPE html>
<html>
<body>

<?php
$name = "Alice";
echo "Hello, " . $name . "!"; // Concatenation
echo "<br>";
echo "Hello, $name!"; // Variable interpolation with double quotes
?>

</body>
</html>

Try it yourself

PHP as a Loosely Typed Language


  • PHP is considered a loosely typed (or dynamically typed) language, meaning you do not have to declare the data type of a variable when you create it.
  • Instead, PHP automatically determines the data type of the variable based on the value it is assigned. This behavior makes PHP flexible and easy to use, especially for beginners in programming.
  • In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.
  • You will learn more about strict and non-strict requirements, and data type declarations in the PHP Functions chapter.


Variable Types


PHP has no command for declaring a variable, and the data type depends on the value of the variable.

Example
<!DOCTYPE html>
<html>
<body>

<?php
$x = 5; // $x is an integer
$y = "John"; // $y is a string

echo $x;
echo $y;
?>

</body>
</html>

Try it yourself

PHP supports the following data types:


  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource


Get the Type


1) gettype()


The gettype() function in PHP is used to get the type of a variable. It returns the data type of the specified variable as a string. This can be helpful for debugging and for checking variable types dynamically.




Example
<!DOCTYPE html>
<html>
<body>

<?php
$var1 = "Hello, world!";
$var2 = 42;
$var3 = 3.14;
$var4 = true;
$var5 = null;

echo gettype($var1)."<br>"; // Outputs: string
echo gettype($var2)."<br>"; // Outputs: integer
echo gettype($var3)."<br>"; // Outputs: double
echo gettype($var4)."<br>"; // Outputs: boolean
echo gettype($var5); // Outputs: NULL

?>

</body>
</html>

Try it yourself

2) var_dump


The var_dump() function in PHP is used to display structured information (including the data type and value) about one or more variables. It is a powerful debugging tool that provides detailed insight into the contents of variables, making it helpful for developers to understand complex data structures.


How var_dump() Works?


      The var_dump() function prints out:

  1. The type of the variable.
  2. The length (if applicable, such as for strings).
  3. The value of the variable.
Example
<!DOCTYPE html>
<html>
<body>

<pre>

<?php
$number = 42;
$text = "Hello, PHP!";
$isTrue = true;
$array = [1, "apple", true];

var_dump($number);
// Output: int(42)

var_dump($text);
// Output: string(11) "Hello, PHP!"

var_dump($isTrue);
// Output: bool(true)

var_dump($array);
// Output:
// array(3) {
// [0]=> int(1)
// [1]=> string(5) "apple"
// [2]=> bool(true)
// }

?>

</pre>

</body>
</html>

Try it yourself

Assign Multiple Values


You can assign the same value to multiple variables in one line:

Example
<!DOCTYPE html>
<html>
<body>

<?php
$x = $y = $z = "Fruit";

echo $x;
echo $y;
echo $z;

?>

</body>
</html>

Try it yourself

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.