Tag Archives: mysql

Else and else if statements in PHP

In the previous tutorial we looked at if statements in PHP. In this tutorial we will look at how to specify more conditions by using the else and else if statements. If you are testing multiple conditions you can use an if statement along with else if and else statements. You can also place an if statement inside another if statement – this is known as a nested if statement.

The basic structure of if, else if and else statements looks like this:

if(condition){
  // do something
}
else if(another condition){
  // do something else
}
else{
  // do something completely different if the
  // other two conditions are not met
}

The basic structure of a nested if statement looks like this:

if(condition){
  if(another condition){
     //do something
  }
}

Make sure you use the correct brackets for conditions and statements inside if statements, and also make sure that you have matching closing brackets.

Watch the video below to see how to use if, else if, and else statements in PHP and then scroll down to see the sample code.

Sample PHP code:

<?php
 
 $age = 20;
 
 if($age >= 18){
 echo "You are old enough to vote";
 }
 
 else if($age == 17){
 echo "You can vote on your next birthday";
 }
 
 else{
 echo "You are not old enough to vote";
 }
 
?>

PHP Manual references:

PHP/MySQL FAQ and Tips

This page provides tips and answers to frequently asked questions on PHP and MySQL.

Why won’t my PHP page load in my browser?

  • Make sure that you have your PHP development environment (such as XAMPP) installed and running.
  • Make sure that your Apache Web Server is also running (In XAMPP, open the XAMPP Manager, click on the Manage Servers tab, select Apache Web Server, and click on Start (and also turn on MySQL Database if you intend to use that).
  • Make sure that the webpage ends with the correct extension (eg. myfile.php) and that it is in the correct directory eg. XAMPP/htdocs/myfile.php.
  • Make sure your htdocs folder is intact. If using XAMPP, you will have a htdocs folder inside the XAMPP installation folder. It will contain all of your PHP files that you write, but it will also contain a lot of other important file such as the configuration file. Do not alter the structure of your htdocs folder or delete/move any other files!

My Apache web server won’t start. Why?

  • Make sure there are no other programs running that may clash with your PHP environment (eg. XAMPP). It may be using the same ports that are require by XAMPP. Skype is known to use the same ports and can cause issue if running. Make sure it is completely closed (go to Task Manager in Windows, select the conflicting program and click End Task; or Force Quit the program on Mac OS).
  • Make sure your htdocs folder is in the correct location and has not been moved or deleted. Make sure the files inside your PHP environment installation’s (eg. XAMPP) folder have not been moved or deleted, including the htdocs folder.
  • Make sure your httpd.conf configuration file points to the correct directory if you have moved your htdocs folder. Make sure you also have privileges to access this folder if you are not an administrator.

I’m getting syntax errors.

  • Check the spelling of your statements or keywords.
  • Make sure your statements all end with a semicolon (;) and that you close brackets and quotation marks where necessary.
  • Make sure you have escaped quotation marks inside strings.
  • Make sure you use commas or periods where need to separate items (eg. in string concatenation).
  • Also make sure that you variable names follow the rules of variable naming.

I get errors when I declare a new variable.

Check the following:

  • Names of variables must begin with a $ dollar sign
  • Letters, numbers and underscores can be used in variable names but no spaces
  • The first chracter of a variable name cannot be a number
  • Variable names are case-sensitive (that means, $myvariable, $myVariable, and $MYVARIABLE are three different variables).

Data types in PHP

This tutorial explains the range of data types that can be used in PHP including:

  • String (letters, numbers and other characters – stored inside quotation marks)
  • Integer (whole positive or negative numbers)
  • Float (positive or negative real numbers – numbers with decimal place)
  • Boolean (can be only one of two possible values – either true or false)
  • NULL (no value / nothing)

String values are always stored inside quotation marks (can be double or single quotation marks, but they must match) eg. $mySentence = “Hello world”;

Integer, float and boolean values are not contained inside quotation marks eg. myNumber = 5;

It is important to note that when working with numbers, if you intend to treat a value as a number (and it will be used for numeric comparisons and calculations) such as a score in a game, then it should be in the form of an integer or float and not stored inside quotation marks. However, if a number value is not going to be used for calculations eg. a phone number or ZIP code, then it can be stored as a string value inside quotation marks.

Watch the video below and scroll down to see the sample code.

PHP sample code:

<?php
 $name = "Batman"; // strings (letters, numbers and other characters)
 $age = 25; // integers (whole positive or negative numbers - no decimal places)
 $num = 30.256; // floats (positive or negative real numbers - can have decimal place)
 $myboolean = true; // Boolean (only one of two possible values - true or false)
 $myvariable = NULL; // NULL (no value / nothing)
?>

PHP Manual references:

Variables in PHP

This tutorial explains what variables are, how to declare variables, and how to assign values to variables in PHP. It also explains how to include variables in an echo statement.

A variable is used to store data in a program. Variables are given a unique name when they are created and can be assigned values of different types. A variable name should not be the same as an existing function that already exists in PHP or the same as another variable that already exists in the program.

Variable names can contain uppercase and lowercase letters and numbers as well as underscores but should start with a letter. In PHP, variable names always start with a $ character eg. $myVariable. Variable names cannot contain spaces.

The main types of data variables can store in PHP are:

  • integer – whole numbers
  • float – numbers with a decimal point
  • string – text which can include letters, numbers, and other characters
  • boolean – true or false values

You can create a variable without immediately giving it a variable, for example: $myVariable; However, variables can be created and assigned a value in one statement eg: $number = 5; or $sentence = “Hello world”;

If you want to give a variable a name which contains more than one word, you can join the words together and use the camel case format where each new word starts with an uppercase letter eg: $myStringVariable;

If you wish to display the value stored in a variable you can use an echo statement eg: echo $myVariable;

Watch the video below to see how to create and use variables, and then scroll down to see the sample code.

There are a few rules to naming variables:

  • Names of variables must begin with a $ dollar sign
  • Letters, numbers and underscores can be used in variable names but no spaces
  • The first character of a variable name cannot be a number
  • Variable names are case-sensitive (that means, $myvariable, $myVariable, and $MYVARIABLE are three different variables).

Sample PHP code:

<?php
 // Variables are given a name after a dollar sign eg. $myvariable
 // Variables are assigned a value after the equals sign (assignment operator)
 // Variable declarations end with a semicolon
 
 $name = "Batman"; // this variable holds a string value
 $age = 25; // this variable holds an integer value
 
 echo "My name is " , $name , " and I am " , $age , " years old."; // concatenated string
?>

The above code is an example of concatenating (joining) a variable and string together. With concatenation, you can join variables and strings in a few different ways. The following different combinations of commas, periods and brackets will work:

<?php
 echo "My name is " , $name , " and I am " , $age , " years old.";
 echo ("My name is " . $name . " and I am " . $age . " years old.");
 echo "My name is " . $name . " and I am " . $age . " years old.";
?>

However, you cannot use the comma method of concatenation if the echo statement is placed inside brackets. Eg. the following will NOT work:

<?php
 echo ("My name is " , $name , " and I am " , $age , " years old.");
?>

Using HTML tags in echo statements and string concatenation

This tutorial explains how to use HTML tags inside the PHP echo statement. It also explains how to join or ‘concatenate’ strings together. The echo statement is not just used to output simple messages or values of variables, but can also be used to output full HTML code which contains HTML tags, as well as CSS code and even JavaScript code!

Watch the video and then scroll down to see the sample code.

PHP sample code:

<?php
 echo "<h3>This is a heading using the HTML h3 heading tag</h3>";
 echo "This string is joined to " , "this other string.";
 echo "</br>This string is on a new line.";
?>

Echo statement and comments

This tutorial introduces the echo statement for displaying messages and also discusses comments that can be added to your code. Echo statements can be used to output simple messages, variable values, and also HTML code. You can even include JavaScript code within echo statements. The echo statement is similar to the print statement you might see in many other languages.

For example, the statement echo “Hello world”; will display the message “Hello world”. The statement echo $myVariable; will output the value of the variable called $myVariable. The statement echo “<h1>My heading</h1>”; will output text formatted in a h1 size heading (using the h1 heading HTML tag).

You could even add JavaScript code to an echo statement, for example: echo ‘<script>alert(“Hello world”);</script>’;

Watch the video below and then scroll down to see the sample code:

PHP sample code:

<?php
 // this is a single-line comment
 
 /* 
 this
 is
 a
 multi-line
 comment
 */
 
 ############################
 # this is a comment block #
 ############################

 echo "Hello world"; // this is a string in an echo statement
 echo "He said \"Hello world\""; // this is an example of escaping quotation marks
 echo 'Hello world'; // this is an example of a string inside single quotation marks
?>

A reminder about the ?> tag

If you are a beginner PHP programmer, it is recommended that you omit the closing ?> PHP tag at the end of your code if the file is pure PHP code. This will prevent any unwanted effects that may occur if you add whitespace after the closing tag.

If you do use the closing ?> then it is important that you do not add any other white space (spaces or new lines) after the tag. You can read the reasons why at the official PHP documentation site.

PHP Manual references:

Getting started with PHP

In this tutorial, you will learn how to setup a PHP development environment on your computer using XAMPP. You will learn how to install and set up XAMPP and test it out so that you are ready for PHP development!

We will be starting out with PHP and working with MySQL a little later on after covering the basics of PHP. These tutorials will be presented on Mac using XAMPP but you are welcome to use Windows or Linux and the steps should be the same or fairly similar for whichever OS you use. You can also try out another PHP development environment but XAMPP is a good choice for all platforms. Click here to download XAMPP.

You can also use a web-based programming environment like Cloud9 to code in PHP.

Watch the video below and then scroll down to see the sample code used in the video.

Sample PHP code:

<?php
 echo "Hello world";
?>

Should I use the closing ?> tag?

If you are a beginner PHP programmer, it is recommended that you omit the closing ?> PHP tag at the end of your code if the file is pure PHP code. This will prevent any unwanted effects that may occur if you add whitespace after the closing tag.

If you do use the closing ?> then it is important that you do not add any other white space (spaces or new lines) after the tag. You can read the reasons why at the official PHP documentation site.

PHP Manual references: