Every time you write php code, you must put it inside the following tags:
//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
<title> Hello World php</title>
#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’;
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 – php tutorial:
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. This is simply a php tutorial and not a book. So, when you are done with this post, head over to PHP String Functions where you will find awesome information on the same subject.
Php Tutorial – Operators:
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.
$x = 10; echo $x + 5; #displays 15
$x = 10; echo $x – 5; #displays 5
$x = 10; echo $x * 2; #displays 20
$x = 20; echo $x / 5; #displays 4
$x = 15; echo $x % 7; #displays 1 (remainder)
$x = 10; echo $x++; #displays 11 (add 1)
$x = 5; echo $x–; #displays 4 (subtract 1)
#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
$x = 10; $y = 30;
$x > 0 && $y < 20; #returns true
$x = 5; $y = 15;
$x == 5 || $x == 15; #returns true
$x = 4; $y = 8;
!($x == $y); #returns true
$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 regarding this php tutorial! Phew! Next time, we will build a simple shopping cart using another php tutorial! 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. Please subscribe.