2. PHP Variables_ Data Types and constants
2. PHP Variables_ Data Types and constants
TYPES AND
CONSTANTS
PHP Variables
• Variables are used to store data, like string of text,
numbers, etc. Variable values can change over the course
of a script. Here're some important things to know about
variables:
<?php
$x = 5;
$y = "John";
echo $x;
echo "<br>";
echo $y;
?>
Naming Conventions for PHP Variables
These are the following rules for naming a PHP variable:
<?php
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
Difference between double and single quotes
• When using double quotes, variables can be inserted to
the string as in the example below:-
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
echo "<h2>$txt1</h2>";
echo "<p>Study PHP at $txt2</p>";
• <?php
$color = "red";
print "Roses are $color";
print "<br>";
print 'Roses are’. $color;
?>
• local
• global
• static
Global and Local Scope
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
myTest();
myTest();
myTest();
Data Types in PHP
• PHP supports total eight primitive data types:
- Integer
- Float
- String
- Boolean
- Array
- Object
- Resource
- NULL
Get the
Type
.
<?php
$x=8;
$y=6.8;
$z="Strings";
var_dump($x);
var_dump($y);
var_dump($z);
?>
Get the Data Type
<!DOCTYPE html>
<html>
<body>
<pre>
<?php
var_dump(5);
var_dump("John");
var_dump(3.14);
var_dump(true);
var_dump([2, 3, 56]);
var_dump(NULL);
?>
</pre>
</body>
</html>
PHP Numbers
• Integer
• Float
• Number Strings
Data types used for numbers:
• In addition, PHP has two more data types used for
numbers:
• Infinity
• NaN
Example of Numbers in PHP
<!DOCTYPE html>
<html>
<body>
<?php
$a = 5;
$b = 5.34;
$c = "25";
var_dump($a);
echo "<br>";
var_dump($b);
echo "<br>";
var_dump($c);
?>
</body>
</html>
Example of Numbers in PHP
<?php
$x=9;
$y=8.9;
$z="76";
echo "The data type of x
is".var_dump($x)."<br>";
echo "The data type of y
is".var_dump($y)."<br>";
echo "The data type of z
is".var_dump($z)."<br>";
?>
PHP Integers
• Integers are whole numbers, without a decimal point (..., -
2, -1, 0, 1, 2, ...).
?>
Check if the type of a variable is integer
<!DOCTYPE html>
<html>
<body>
<?php
// Check if the type of a variable is integer
$x = 5985;
var_dump(is_int($x));
echo "<br>";
// Check again...
$x = 59.85;
var_dump(is_int($x));
?>
</body>
</html>
SIZE OF AN INTEGER
<?php
$x=5;
if(is_int($x)){
echo $x."is an integer";
ECHO "<BR>";
}
echo "The size of the integer x is".
PHP_INT_SIZE;//On most 64-bit systems,
PHP_INT_SIZE will return 8, meaning that an
integer is 8 bytes
?>
PHP Floating Point Numbers or Doubles
• Floating point numbers (also known as "floats", "doubles",
or "real numbers") are decimal or fractional numbers
Check if the type of a variable is float
<!DOCTYPE html>
<html>
<body>
<?php
// Check if the type of a variable is float
$x = 10.365;
var_dump(is_float($x));
?>
</body>
</html>
Check if a numeric value is finite or infinite
<!DOCTYPE html>
<html>
<body>
<?php
// Check if a numeric value is finite or infinite
$x = 1.9e411;
var_dump($x);
?>
</body>
</html>
Check if the variable is numeric
<!DOCTYPE html>
<html>
<body>
<?php
// Check if the variable is numeric
$x = 5985;
var_dump(is_numeric($x));
echo "<br>";
$x = "5985";
var_dump(is_numeric($x));
echo "<br>";
$x = "59.85" + 100;
var_dump(is_numeric($x));
echo "<br>";
$x = "Hello";
var_dump(is_numeric($x));
?>
</body>
</html>
INFINTE and NAN
<?php
// Demonstrating Positive Infinity
$positive_infinity = 1.0 / 0.0; // Dividing by zero results in
infinity
echo "Positive Infinity: $positive_infinity\n"; // Outputs:
Positive Infinity: INF
// Demonstrating Negative Infinity
$negative_infinity = -1.0 / 0.0; // Negative number divided by
zero results in negative infinity
echo "Negative Infinity: $negative_infinity\n"; // Outputs:
Negative Infinity: -INF
?>
Fatal error: Uncaught DivisionByZeroError: Division by zero in C:\
xampp\htdocs\PHP-Practice\13Infandnan.php:3 Stack trace: #0
{main} thrown in C:\xampp\htdocs\PHP-Practice\13Infandnan.php
on line 3
<!DOCTYPE html>
<html>
<body>
<?php
echo(pi());
?>
</body>
</html>
PHP min() and max() Functions
<!DOCTYPE html>
<html>
<body>
<?php
echo(min(0, 150, 30, 20, -8, -200) . "<br>");
echo(max(0, 150, 30, 20, -8, -200));
?>
</body>
</html>
PHP abs() Function
<?php
echo(abs(-6.7));
?>
</body>
</html>
PHP sqrt() Function
<!DOCTYPE html>
<html>
<body>
<?php
echo(sqrt(64) . "<br>");
echo(sqrt(0) . "<br>");
echo(sqrt(1) . "<br>");
echo(sqrt(9));
?>
</body>
</html>
PHP round() Function
The round() function rounds a floating-point number to its nearest integer:
<!DOCTYPE html>
<html>
<body>
<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
</body>
</html>
Random Numbers
define(name, value)
OUTPUT:
Good Evening
Constants(contd.)
• PHP constant: const keyword - PHP introduced a
keyword const to create a constant. The const keyword
defines constants at compile time. It is a language
construct, not a function. The constant defined using
const keyword are case-sensitive.
<?php
const WISHES="Good day";
echo WISHES;
?>
OUTPUT:
Good day
Constants(contd.)
• Constant() function - There is another way to print the
value of constants using constant() function.
constant (name)
Constants(contd.)
<?php
define("WISHES", "Good Evening");
echo WISHES. "<br>";
echo constant("WISHES"); //print the value of the
constant
//both are similar
?>
OUTPUT:
Good Evening
Good Evening
Constants(contd.)
• PHP Constant Arrays - In PHP, you can create an Array
constant using the define() function.
<?php
define("courses", [
"PHP",
"HTML",
"CSS"
]);
echo courses[0];
?>
OUTPUT:
PHP
Constants(contd.)
• PHP Constant Arrays - In PHP, you can also create an
Array constant using const keyword.
<?php
const WISHES=array("PHP",
"HTML",
"CSS");
echo WISHES[0];
?>
OUTPUT:
PHP
Constants(contd.)
• Constants are Global - Constants are automatically
global and can be used across the entire script.
<?php
define("WISHES", "Good Evening");
function test() {
echo WISHES;
}
test();
?>
OUTPUT:
Good Evening
Example of Constants
• <?php
• // Using constants
• $radius = 5;
• $circumference = 2 * PI * $radius;
• ?>
Defining Color Constants
• <?php
• ?>