To kick off my php learning journey, I am going to start with some syntax then jump straight into building a simple shopping cart. Sounds exciting right? So, let us get started. Note: if you have used other languages like Java or Javascript, you will find some syntax used in PHP quite familiar. If this is your first language, do not worry, it is actually easier than you think: One more warning: The reason why I will not spend so much time teaching syntax is because there are more than enough tutorials online that do just that and I didn’t want to create yet another one!
Every time you write php code, you must put it inside the following tags:
<?php
//Your PHP code goes right here!
?>
Remember to end all php files with a .php extension for example if we had a file called index, then we would save it as index.php. Easy breezy right?
Next: let us create our first “Hello, World” page using php. So what do we need? Well, we have already learned how to include php inside the tags, now we need to put everything we have inside an HTML page! Take a look at this: index.php
<!DOCTYPE html>
<html>
<head>
<title> Hello World php</title>
</head>
<body>
<?php
#this is php code inside html
#Remember to end statements with semi-colons.
echo 'Hello, world';
echo 'This is my first web page using php';
?>
</body>
</html>
I am suspecting you have already guessed what our web page will look like. So, what does ‘echo’ do? It simply prints to the page(no pun intended). One other thing to know is that there is a ‘print’ method too, that does the same thing; prints to the page whatever is to the right of it. I hope you are following along with your own code.
Variables in php:
All php variables start with a dollar sign ($) or they will not work! Let us take a look at some variables before we start doing other things:
$age = 20; #stores integers
$price = 2.50; #stores doubles
$weight = 150;
$first_name = 'Johnny'; #stores a string - 'Johnny'
$is_qualified = false; #stores a boolean - false
$registered = true; #stores a boolean - true
/*
$age is not the same as $Age ----------
because variables are: case-sensitive
This is another way of commenting in php
*/
echo $age; #this will show 20, try it.
echo $weight; #this will show 150, go try it.
echo $first_name; #Johnny
print $registered; #true
/***********
what will this print? Lesson: we use (.) dot operator to join
strings with other strings or variables
***********/
echo 'I am '.$age.' years old and weigh '.$weight.' pounds';
At this point, it is good to ask yourself(especially if you have used Java or C++ before), how does the PHP engine know the type of the variable? The answer is that you do not need to declare the type of a variable in php because the engine determines it for you. It simply checks the value of the variable and that does the trick. The same thing happens in Python!
String Manipulation in PHP:
There are several methods that we can use to manipulate strings. I will step through just a few of them for now. I am really trying to avoid making the longest post ever. Let us jump to it:
$greeting = "good morning php students";
#Now let us find the length of that greeting
echo strlen($greeting); #25?, try it.
#Now let us turn all words that start with lowercase
#letters into capital letters
echo ucwords($greeting); #prints 'Good Morning Php Students'
#Now let us make all letters uppercase
echo strtoupper($greeting); #GOOD MORNING PHP STUDENTS
#Now let us make all letters lowercase
echo strtolower("NEW YORK"); #new york
There are more methods that you could explore further but I am not going to create another PHP documentation here. So, when you are done with this post, head over to PHP String Functions where you will find awesome information on the same subject.
Operators in PHP:
There is always need for math: In php, there are four kinds of operators and they include the following:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators.
I will try to go through them here but it is a good idea to practice using them. I guess they didn’t lie when they said practice makes perfect! I will not exhaust the list here.
Arithmetic Operators:
#addition (+)
$x = 10; echo $x + 5; #displays 15
#subtraction (-)
$x = 10; echo $x - 5; #displays 5
#multiplication (*)
$x = 10; echo $x * 2; #displays 20
#divide (/)
$x = 20; echo $x / 5; #displays 4
#modulus (%)
$x = 15; echo $x % 7; #displays 1 (remainder)
#increment (++)
$x = 10; echo $x++; #displays 11 (add 1)
#decrement (--)
$x = 5; echo $x--; #displays 4 (subtract 1)
Comparison Operators:
#is equal to (==)
10 == 10; #returns true
#is not equal to (!=)
3 == 10; #returns false
4 != 50; #returns true
10 != 0; #returns true
#is not equal to (<>) same as !=
3 <> 4; #returns true
15 <> 15; #returns false
#greater than (>)
10 > 1; #returns true
100 > 150; #returns false
#greater than or equal to (>=)
10 >= 10; #returns true
15 >= 20; #returns false
#less than (<)
23 < 100; #returns true
10 < 4; #returns false
#less than or equal (<=)
15 <= 15; #returns true
14 <= 10; #returns false
Logical Operators:
#and (&&)
$x = 10; $y = 30;
$x > 0 && $y < 20; #returns true
#or (||)
$x = 5; $y = 15;
$x == 5 || $x == 15; #returns true
#not (!)
$x = 4; $y = 8;
!($x == $y); #returns true
Assignment Operators:
# (=)
$x = 13; #assigns 13 to $x
# (+=)
$x += 10; #same as $x = $x + 10;
# (-=)
$x -= 10; #same as $x = $x - 10;
# (*=)
$x *= 2; #same as $x = $x * 2;
# (/=)
$x /= 10; #same as $x = $x / 10;
# (.=)
$x .= $y; #same as $x = $x. $y (string joining)
# (%=)
$x %= 7; #same as $x = $x % 7;
That is enough for today! Phew! Next time, we will build a simple shopping cart using php! I am excited already and I hope you are too!
If you have questions, please let me know and I will be glad to help. Thanks for stopping by.