Tutorial Title: Data Types and More in PHP
Course: PHP
Instructor: Muhammad Samim
1. Variables in PHP
Course: PHP
Instructor: Muhammad Samim
1. Variables in PHP
- Variable means container which keeps the data. Different types of Variables store different types of data
- Variables in PHP are case sensitive
- Variable starts with $ symbol in PHP
- Variable cant be started with numbers
- Variable can't have -
- $variableName = "Muhammad Samim"; //String data
- $variableName = 200; //Numbers
- $variableName = 200.5; //Float
- echo $variableName; //Displays whats inside variable
- echo $variableName . $variableName; //Dot will concatenate two variables
- echo $variableName . " " . $variablename; //Will give space between data of two variable while displaying.
- PHP string variable can store HTML tags inside e.g $variableName = "<h1>Muhammad Samim </h1>";
2. Math in PHP
- Precedence applies in doing maths in PHP
- echo 200 / 200; //Displays the result of Division
- echo 200 * 200; //Displays the result of Multiplication
- echo 200 + 200; //Displays the result of Addition
- echo 200 - 200; //Displays the result of Subtraction
- echo 200 + 200 / 200; //Displays the result, first it will be divided then will be added due to rule of precedence
- echo (200 + 200) / 200; //Displays the result, here first will be added then will be divided due to rule of precedence as Parenthesis priority is in first than Division
- $variableName1 = 200;
- $variableName2 = 300;
- echo $variableName1 + $variableName2; //Displays the result of two variables
3. Array
- Array stores same or different types of data in one variable by assigning index number to each value in PHP
- $arrayName = array(200,400.3,"Muhammad Samim",500);
- or $arrayname = [200,400.3,"Muhammad Samim",500]; // Both are same
- echo $arrayName[indexNumber]; //Displays the value of given index number
- print_r($arrayName); //Displays the complete structure of array with index numbers and their values.
4. Associative Array
- Its same like regular array we knew above but here we can give code or name instead of index number
- $arrayName = array("firstName" => "Muhammad", "lastName" => "Samim", "Number" => "07"); //Associating value with names of code instead of index number
- echo $arrayName["firstName"] . " " . $arrrayName["lastName"] . " " . $arrayName["number"]; //Displays the data of array by associated names
- print_r($arrayName); //Displays the complete structure of array
Comments
Post a Comment