A really brief introduction to PHP syntax

In this php tutorial, 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!

php tutorial

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

<!DOCTYPE html>
<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:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. 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 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.

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

4 Comments on “A really brief introduction to PHP syntax

  1. Brunoais Reply

    January 17, 2013 at 11:10

    Some mistakes here:
    “#increment (++)
    17 $x = 10; echo $x++; #displays 11 (add 1)”

    Displays 10, actually.
    If you want to display 11, then you need:
    $x = 10; echo ++$x;
    (same applies to the one about the –)

    I think you should also mention other operators, and not only those before the shopping cart.

    Talk about the:
    $talk[] = “talk1”; // append “talk1” to the array talk
    $talk = array(“talk1”); // create an array with only “talk1”

    And also say that arrays in php are hash tables. I think those are quite useful to get things started.

    • joylaxpower Reply

      January 17, 2013 at 3:12

      Thanks for finding that error there. I don’t know why I missed it. I guess what I was really trying to get at was the fact that a post-increment adds 1 but I should have mentioned that it happens after the operation. Also thanks for mentioning the arrays and whatnot.
      Thanks for taking your time to read through.

  2. Elton Sanchez Reply

    January 25, 2013 at 5:58

    Not bad. I think it’s also important to mention string concatenation operators and the differences in using single-quoted literal strings and double-quoted strings, plus syntax rules in using variables in such strings.


    $a = ‘Hello’; //strings may be double-quoted or single quoted, but have differences (see below0
    $b = “World”;
    $hw = $a . ‘, ‘ . $b; // . is the concatenation operator for strings
    echo “$hw!”; //prints Hello, World!
    echo ‘$hw!’; //prints $hw!

    echo “$hw!n”; //prints Hello, World! and adds a new line
    echo ‘$hw!n’; //prints $hw!n

    • joylaxpower Reply

      January 25, 2013 at 7:22

      Thank you for your input. I really appreciate that and I know I didn’t exhaust the entire concepts.

      Thanks again and hope to see you around!

Leave a Reply

Your email address will not be published. Required fields are marked *