What are Arrays in PHP?

  1. 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.
  2. In variables as you know we can store data but with a limitation of only one single data or value.
  3. I would say Arrays are variables with Superpowers! It’s the number 1 container in programming.
  4. Let’s open our code editor and start with a blank page – Open HTML, head, body, and PHP tags.
  5. Next step – write a variable. Example – $numberList
  6. To make this variable a array simply apply a array function i.e.  $numberList = array(); or $numberList = []; syntax array() or []
  7. Now simply try storing different values e.g. $numberList = array(25,264,52,’99’,348, ‘<h1>Hello</h1>’);
  8. 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.
  9. 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
  10. Next how do we display array data? Answer – example echo $numberList [0];
  11. With this, you will be able to see value 25 displayed.
  12. Try changing the index value to see the displayed value change.
  13. Hope you had fun learning. Thank you for your time.

How to do Math in PHP

  1. In addition to Variables in PHP today we are going to talk about Math
  2. Yes, you can do calculations in PHP similar to what you do on your calculator
  3. Open your code editor and open a new file.
  4. Open an HTML, HEAD, and PHP Tags
  5. Between the PHP tags,
  6. You can do additions, subtractions, division, and multiplications
  7. For every function, use echo and then simply add, multiply or subtract. 
  8. Syntax for functions – (+ for addition), (- for subtraction), (* for multiplication) and (/ for division)
  9. We can also do calculations with multiple functions such as “echo 45 + 34 * 45 / 421 – 45;”
  10. I would like to also share with you on the order of operations. Please remember that multiplication and division go before addition and subtraction.
  11. For example – echo 5 + 5 * 10 will show as 55 and not 100 unless you place 5+5 in prentices like (5 + 5) * 10
  12. 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?

  1. What is a variable:

Variables are like containers.

Elements used to store data

Use that data with Variable name

2. Example 

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;
  1. Variables are case sensitive and will end up making them a different variable if different case.
  1. Data in variable – We can store many different data types
  1. String data type – Always in quotes (texts)
  2. Numbers – Does not have quotes
  3. Floating point numbers – ones with decimals. (takes more memory)
  1. Concatenation – . Dot between two variables will smash them together
  1. Assign HTML tag to a variable = $name = “<h1> HELLO</h1>”
  1. Imp: Can only assign one value to the variables.