Laravel
308
w3alert.com
24-12-2019
PHP provides several data types that can be stored data in variables. PHP has allowed eight types of data types.
there are five simple data types in PHP like String, Integer, Float, Boolean, null and the remaining three are compound data types like Array, Object, Resource.
NOTE:- data types are used to store different types of data in PHP.
In PHP, string can be used to store characters, alphabet, numbers. Or A string is a group of special characters (#,*,&,%,...), letters (a,b,c,d,...) and numbers (1,2,3,4,...) like: "Hello w3alert!".
NOTE:- String can be lengthy up to 2GB (2147483647 bytes max.).
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Strings Examples</title> </head> <body> <?php $num1 = "Hello w3alert!"; echo $num1; echo "<br>"; $num2 = "I am a developer."; echo $num2; echo "<br>"; ?> </body> </html>
Integers can have both zero, positive (1,2,3,...) and negative (-1,-2,-3,...) values but no decimal values (1.2,3.4,4.50,...).
Integers can be specified in decimal (base 10), hexadecimal (base 16 - prefixed with 0x) or octal (base 8 - prefixed with 0) notation, optionally preceded by a sign (- or +).
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Integer Examples</title> </head> <body> <?php $num1 = 127; // decimal number var_dump($num1); echo "<br>"; $num2 = -150; // a negative number var_dump($num2); echo "<br>"; $num3 = F5A; // hexadecimal number var_dump($num3); echo "<br>"; $num4 = 567; // octal number var_dump($num4); ?> </body> </html>
In PHP,Float is used to hold decimal/float/double/real numbers.
Floating point numbers (Floats, Doubles, Real Numbers) are decimal numbers like: 3.4, 1.47, 5.8,... .
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Floats Examples</title> </head> <body> <?php $num1 = 1.564; var_dump($num1); echo "<br>"; $num2 = 67.23; var_dump($num2); echo "<br>"; $num3 = 5.8; var_dump($num3); ?> </body> </html>
A Boolean data type works with two possible conditions such as 1 (true) or 0 (false).
If a program is error free, it returns the true value 1 and if the program has some objections, it returns the false value 0.
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Booleans Examples</title> </head> <body> <?php // Assign the value TRUE to a variable $num1 = true; if($num1 == TRUE) echo "Return value TRUE"; else echo "Return value False"; echo "<br>"; // Assign the value False to a variable $num2 = true; if($num2 == False) echo "Return value True"; else echo "Return value False"; ?> </body> </html>
The php array holds one or more than one value in a PHP variable. There are 3 types of array in PHP like: Indexed Array, Associative Array, Multidimensional Array.
you can define array from two types:
1. []; //it is a first way.
2. array(); //it is a second way.
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Arrays</title> </head> <body> <?php $fruits = array("mango", "apple", "orange"); var_dump($fruits); echo "<br>"; $Vegetable = ["carrots", "beans", "leeks"]; var_dump($fruits); ?> </body> </html>
An object data type holds data and Information on how to process that data. Objects are different from other PHP data types. Because they hold not only the data itself but also all the information on how that particular data can be processed.
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Object Examples</title> </head> <body> <?php // Class definition class welcome{ // properties public $str = "Hello w3alert!"; // methods function show_welcome(){ return $this->str; } } // Create object from class $message = new welcome; var_dump($message); ?> </body> </html>
Data type null in php, which has no value in the variable or This is a special type of data type that holds the null value in the PHP variable.
For e.g. When a variable is defined in php but does not assign any value to it, it will be automatically stored NULL in it.
<!DOCTYPE html> <html lang="en"> <head> <title>PHP NULL Value Example</title> </head> <body> <?php $x = NULL; var_dump($x); echo "<br>"; $y = "Hello w3alert!"; $y = NULL; var_dump($y); ?> </body> </html>
The php resource datatype is not the original data type. Because it is used to store references to other functions and external resources.
<?php $host='localhost'; $username='root'; $password=''; $dbname = "test"; $connection = mysqli_connect($host,$username,$password,"$dbname"); if(!$connection) { die('Could not Connect MySql Server:' .mysql_error()); } ?> The above function will return a resource type data to be stored into $connection variable.