A Crash course on web development with PHP for beginners (Day 2)

Hi again! Hope you got a good chew on the last tutorial? If you missed day 1 of this crash course you might want to check it out. If you got a good hand on the last tutorial then you now know how to write a simple “Hello world” program, create variables and do a little calculation with PHP.

Great! Let’s do some cooler stuff. Well a little technical though.

A few things you need to know

Constants: they are similar to variables. I define constants as variables that do not change. After you set the value of a constant, its stays the same.

Values stored in a variable or a constant are stored as a specific type of data. PHP provides eight data types:

Integer: A whole number

Floating-point number: A numeric value with decimal digits

String: A series of characters

Boolean: A value that can be either true or false

Array: A group of values in one variable

Object: A structure created with a class

Resource: A reference that identifies a connection

NULL: A value that represents no value

Let’s get started

Just as we did in the previous tutorial, we will create a folder sacc02 in our www directory. Create an index.php file in the folder.

Before we go on, here are a few rules on creating variables:

  • All variable names have a dollar sign ($) in front of them. This tells PHP that it is a variable name.
  • Variable names can be any length.
  • Variable names can include letters, numbers, and underscores only.
  • Variable names must begin with a letter or an underscore. They cannot begin with a number.
  • Uppercase and lowercase letters are not the same. For example, $firstname and $Firstname are not the same variable. If you store information in $firstname, for example, you can’t access that information by using the variable name $firstName.

I recommend using uppercase or underscore to separate words in variables for easy readability and Capital letters for constants (though this does not affect the efficiency of your code in anyway so you can ignore my recommendation). For example: $firstName, $first_name, $lastName, $last_name, CONSTANT.

Fortunately unlike other languages like C, Java, VB where you need to declare variables with their data types PHP does it all with the “$” symbol. Copy the code snippet below into your index.php and study how it works.

<?php
 // declare a constant company with the value StackArena
 define("COMPANY","StackArena");
 echo "My company's name is ".COMPANY;
  
 echo "<br />";
  
 // constant age with value 29
 define ("AGE",29);
 echo "I am ".AGE." years old!";
  
 echo "<br />";
  
 // a string variable
  $str = "My company's name is ";
 echo $str.COMPANY;
  
 echo "<br />";
  
 // escape single quote with the "\" symbol to avoid error.
 // $string = 'It is Sally's house'; gives an error
 $string = 'It is Sally\'s house';
 echo $string;
  
 echo "<br />";
?>
<?php  
 // use double quote to wrap single quotes and vice versa
 $string1 = "It is Sally's house";
 $string2 = '"It is a bird!"';
  
 echo $string1."<br />";

 echo $string2;
  
 echo $string1."<br />";
  
 // joining strings
 $stringall = “Hello”;
 $stringall .= “ World!”;
 echo $stringall;
?>

The break in the tags shows you that you can write PHP codes anywhere in an HTML page. Add the snippet below to your index.php file.

<p><strong><?php echo "A Crash course on web development with PHP for beginners (Day 2)"; ?></strong></p>

<p><em><?php echo "Put your name here..."; ?></em></p>

If you are also new to HTML, the <p> tag stands for paragraph, the <strong> tag stands for bold and the <em> tag stands for italic. The </ tag name> closes each tag – where tag name can be p, strong, em or any other tag.

Conditional Statements

These might as well be the most important tool in programming irrespective of language. It’s how you tell the computer to do something based on a condition. Let’s create a simple scenario.

Suppose it’s raining heavily and you wanted to go to school. You could make a statement like “I will go to school if the rain stops” or in the passive sense “If the rain stops, then I will go to school”

Programming works in a similar manner. The statement “If the rain stops, then I will go to school” would translate to in C, Java and PHP

If(the rain stops)
{
 then I will go to school;
}

That brings us to the Boolean data type – “the rain stops” can either be true or false. So we could write something like

<?php
 // Boolean variable can either be TRUE or FALSE though 0 could represent FALSE and any other number could represent true such as 1. So replacing TRUE with 1 produces the same effect
 $the_rain_stops = TRUE;
  
 If($the_rain_stops)
 {
   echo "then I will go to school";
 }
?>

Here is a conversation between Akin and John:

Akin: If the rain stops before 9 we will go to school
John: And if it doesn’t?
Akin: Then we’ll watch a movie.

Translating the resulting action in PHP would be

<?php
 // $the_rain_stops represents the rain stopping before 9
 $the_rain_stops = TRUE;
   
 If($the_rain_stops)
 {
   echo "then we will go to school";
 }
 else  // if the rain doesn’t stop
 {
   echo "then Then we’ll watch a movie"; 
 }
?>

Comparison Operators

Comparison Operators are used for comparing variables. Below is a list of operators in PHP

Operator Meaning
== Are the two values equal in value?
=== Are the two values equal in both value and data type?
> Is the first value larger than the second value?
>= Is the first value larger than or equal to the second value?
< Is the first value smaller than the second value?
<= Is the first value smaller than or equal to the second value?
!=, <> Are the two values not equal to each other in value?
!== Are the two values not equal to each other in either value or data type?

You could use the comparison operators in the if statement like this:
if($variable1 == $variable2)

Let’s take a break here and let you play around with the operators and compare different variables.

Have fun!

Day 3

A Web designer and developer. Speaks PHP, Java, Javascript, HTML

Leave a Reply

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