Unit 1 PHP-1
Unit 1 PHP-1
Variables can store data of different types, and different data types can do different things.
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Resource
Getting the Data Type
You can get the data type of any object by using the var_dump() function.
Example
The var_dump() function returns the data type and the value:
$x = 5;
var_dump($x); O/P : int(5)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
var_dump($x);
?>
</body>
</html>
PHP String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
$x = "Hello world!";
$y = 'Hello world!';
var_dump($x);
echo "<br>";
var_dump($y);
O/P:
string(12) "Hello world!"
string(12) "Hello world!"
PHP Integer
In the following example $x is an integer. The PHP var_dump() function returns the data type and
value:
Example
$x = 5985;
var_dump($x);
PHP Float
A float (floating point number) is a number with a decimal point or a number in exponential form.
In the following example $x is a float. The PHP var_dump() function returns the data type and
value:
Example
$x = 10.365;
var_dump($x);
O/P:
float(10.365)
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional
testing.
Example
$x = true;
var_dump($x);
O/P:
bool(true)
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump() function returns the data type and
value:
Example
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
O/P:
array(3)
{
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
}
PHP Object
Classes and objects are the two main aspects of object-oriented programming.
When the individual objects are created, they inherit all the properties and behaviors from the class,
but each object will have different values for the properties.
Let's assume we have a class named Car that can have properties like model, color, etc. We can
define variables like $model, $color, and so on, to hold the values of these properties.
When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties
and behaviors from the class, but each object will have different values for the properties.
A variable of data type NULL is a variable that has no value assigned to it.
Example
$x = "Hello world!";
$x = null;
var_dump($x);
O/P:
NULL
If you assign a string to the same variable, the type will change to a string:
Example
$x = 5;
var_dump($x);
$x = "Hello";
var_dump($x);
O/P:
int(5)
string(5) "Hello"
If you want to change the data type of an existing variable, but not by changing the value, you can
use casting.
$x = (string) $x;
var_dump($x);
O/P:
string(1) "5"
PHP Resource
The special resource type is not an actual data type. It is the storing of a reference to functions and
resources external to PHP.
We will not talk about the resource type here, since it is an advanced topic.
PHP Casting
Sometimes you need to change a variable from one data type into another, and sometimes you want a
variable to have a specific data type. This can be done with casting.
Cast to String
To cast to string, use the (string) statement:
Example
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;
//To verify the type of any object in PHP, use the var_dump() function:
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
O/P:
string(1) "5"
string(4) "5.34"
string(5) "hello"
string(1) "1"
string(0) ""
Cast to Integer
To cast to integer, use the (int) statement:
$a = 5; // Integer
$b = 5.34; // Float
$f = true; // Boolean
$g = NULL; // NULL
$a = (int) $a;
$b = (int) $b;
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
$f = (int) $f;
$g = (int) $g;
O/P:
int(5)
int(5)
int(25)
int(0)
int(0)
int(1)
int(0)
Cast to Float
To cast to float, use the (float) statement:
$a = 5; // Integer
$b = 5.34; // Float
$e = "hello"; // String
$f = true; // Boolean
$g = NULL; // NULL
$a = (float) $a;
$b = (float) $b;
$c = (float) $c;
$d = (float) $d;
$e = (float) $e;
$f = (float) $f;
$g = (float) $g;
O/P:
float(5)
float(5.34)
float(25)
float(0)
float(0)
float(1)
float(0)
Cast to Boolean
To cast to boolean, use the (bool) statement:
$a = 5; // Integer
$b = 5.34; // Float
$c = 0; // Integer
$d = -1; // Integer
$e = 0.1; // Float
$f = "hello"; // String
$g = ""; // String
$h = true; // Boolean
$i = NULL; // NULL
$a = (bool) $a;
$b = (bool) $b;
$c = (bool) $c;
$d = (bool) $d;
$e = (bool) $e;
$f = (bool) $f;
$g = (bool) $g;
$h = (bool) $h;
$i = (bool) $i;
O/P:
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
If a value is 0, NULL, false, or empty, the (bool) converts it into false, otherwise true.
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
Cast to Array
To cast to array, use the (array) statement:
$a = (array) $a;
$b = (array) $b;
$c = (array) $c;
$d = (array) $d;
$e = (array) $e;
O/P:
array(1) {
[0]=>
int(5)
}
array(1) {
[0]=>
float(5.34)
}
array(1) {
[0]=>
string(5) "hello"
}
array(1) {
[0]=>
bool(true)
}
array(0) {
}
PHP Constants
Constants are like variables, except that once they are defined they cannot be changed or
undefined.
PHP Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the entire script.
Syntax
define(name, value);
Parameters:
Example
Create a constant with a case-sensitive name:
echo GREETING;
Welcome to W3Schools.com!
Example
Create a case-sensitive constant with the const keyword:
echo MYCAR;
O/P:
Volvo
const cannot be created inside another block scope, like inside a function or inside
an if statement.
define can be created inside another block scope.
function myTest()
echo GREETING;
myTest();
O/P:
Welcome to W3Schools.com!
These magic constants are written with a double underscore at the start and the end, except for the
ClassName::class constant.
Magic Constants
Here are the magic constants, with descriptions and examples:
Constant Description
return __CLASS__;
}
echo $kiwi->myValue();
?>
O/P Fruit
<?php
__DIR__ The directory of the file. echo __DIR__;
?>
C:\awesomesites\w3schools\php
<?php
__FILE__ The file name including the echo __FILE__;
full path. ?>
C:\awesomesites\w3schools\php\
magic_const_file.php
return __FUNCTION__;
echo myValue();
?>
O/P: myValue
<html>
<body>
<?php
echo __LINE__;
?>
</body>
</html>
O/P:
return __METHOD__;
echo $kiwi->myValue();
?>
O/P;
Fruits::myValue
return __NAMESPACE__;
?>
<?php
echo myValue();
?>
myArea
<?php
ClassName::class Returns the name of the namespace myArea;
specified class and the class Fruits {
name of the namespace, if public function myValue(){
any. return Fruits::class;
}
}
?>
<?php
$kiwi = new Fruits();
echo $kiwi->myValue();
?>
myArea\Fruits
Note:
The magic constants are case-insensitive, meaning __LINE__ returns the same as __line__.
PHP Operators
Operators are used to perform operations on variables and values.
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of
the assignment expression on the right.
x=y x=y The left operand gets set to the value of the
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
ADVERTISEMENT
or Or $x or $y True if either $x or $y is
true
|| Or $x || $y True if either $x or $y is
true
=== Identity $x === Returns true if $x and $y have the same key/value pairs in the
$y same order and of the same types
if (5 > 3) {
Example
Check if $a is greater than $b, AND if $a is less than $c:
$a = 200;
$b = 33;
$c = 500;
if ($a > $b && $a < $c ) {
Example
Output "Have a good day!" if the current time is less than 20, and "Have a good
night!" otherwise:
$t = date("H");
} else {
Example
Output "Have a good morning!" if the current time is less than 10, and "Have a
good day!" if the current time is less than 20. Otherwise it will output "Have a
good night!":
$t = date("H");
Short Hand If
To write shorter code, you can write if statements on one line.
Example
One-line if statement:
$a = 5;
echo $b
Example
One-line if...else statement:
$a = 13;
echo $b;
Nested If
You can have if statements inside if statements, this is
called nested if statements.
Example
An if inside an if:
$a = 13;
} else {
Syntax
switch (expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
case label3:
//code block
break;
default:
//code block
Example
More than one case for each code block:
$d = 3;
switch ($d) {
case 1:
case 2:
case 3:
case 4:
case 5:
break;
case 6:
case 0:
echo "Weekends are the best!";
break;
default:
PHP Loops
In the following chapters you will learn how to repeat code by using loops in
PHP.
PHP Loops
Often when you write code, you want the same block of code to run over and
over again a certain number of times. So, instead of adding several almost equal
code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a
certain condition is true.
The following chapters will explain and give examples of each loop type.
$i = 1;
echo $i;
$i++;
Alternative Syntax
The while loop syntax can also be written with the endwhile statement like
this
Example
Print $i as long as $i is less than 6:
$i = 1;
echo $i;
$i++;
endwhile;
Example
Print $i as long as $i is less than 6:
$i = 1;
do {
echo $i;
$i++;
Example
Print the numbers from 0 to 10:
Example
Loop through the items of an indexed array: