- Today we will be learning Arrays in PHP. With the help of Arrays, we can store multiple types of data in one container. There are different types of Arrays.
- In variables as you know we can store data but with a limitation of only one single data or value.
- I would say Arrays are variables with Superpowers! It’s the number 1 container in programming.
- Let’s open our code editor and start with a blank page – Open HTML, head, body, and PHP tags.
- Next step – write a variable. Example – $numberList
- To make this variable a array simply apply a array function i.e. $numberList = array(); or $numberList = ; syntax array() or 
- Now simply try storing different values e.g. $numberList = array(25,264,52,’99’,348, ‘<h1>Hello</h1>’);
- Now carefully note that each value in the array is called index. And an index in the array starts with 0 which means in our example 25 is index 0, 264 is index 1, and so on.
- To see the structure of the array. There is a built-in function in PHP to see the structure. Try the “print_r” function to check the structure of the array
- Next how do we display array data? Answer – example echo $numberList ;
- With this, you will be able to see value 25 displayed.
- Try changing the index value to see the displayed value change.
- Hope you had fun learning. Thank you for your time.
How to do Math in PHP
- In addition to Variables in PHP today we are going to talk about Math
- Yes, you can do calculations in PHP similar to what you do on your calculator
- Open your code editor and open a new file.
- Open an HTML, HEAD, and PHP Tags
- Between the PHP tags,
- You can do additions, subtractions, division, and multiplications
- For every function, use echo and then simply add, multiply or subtract.
- Syntax for functions – (+ for addition), (- for subtraction), (* for multiplication) and (/ for division)
- We can also do calculations with multiple functions such as “echo 45 + 34 * 45 / 421 – 45;”
- I would like to also share with you on the order of operations. Please remember that multiplication and division go before addition and subtraction.
- For example – echo 5 + 5 * 10 will show as 55 and not 100 unless you place 5+5 in prentices like (5 + 5) * 10
- Also – You can save numbers as variables and use them for calculations. For example – $number1 = 12; and $number2 = 24;. Echo $number1 + $number2;
What are the Variables in PHP?
- What is a variable:
Variables are like containers.
Elements used to store data
Use that data with Variable name
Open a blank code editor
Open doc type HTML
Open and close PHP tag <?php ?>
Used the $ sign followed by the name of the variable and then ;
$name = ‘Ratan’; for numbers $numbers = 100;
- How can we use the variable?
- echo $name;
- Variables are case sensitive and will end up making them a different variable if different case.
- Data in variable – We can store many different data types
- String data type – Always in quotes (texts)
- Numbers – Does not have quotes
- Floating point numbers – ones with decimals. (takes more memory)
- Concatenation – . Dot between two variables will smash them together
- Assign HTML tag to a variable = $name = “<h1> HELLO</h1>”
- Imp: Can only assign one value to the variables.