The Basics of PHP
Progress:

Introduction

Lessons

Quiz

Resources

You have completed the tutorial and finished the quiz! Congratulations!
Please review unchecked information to view quiz results.

Introduction

The goal of this tutorial would be to demonstrate the ease of use and simplicity in developing web sites and applications with PHP as a viable server side web technology. In this tutorial we will cover some basic code snippets and example usage of the code in action. We will go over how it interacts with the web server and touch upon the concepts and methodology of using PHP in the real world for web applications and web sites.  This tutorial will cover syntax, variables, functions and output.

The basic concepts, syntax and uses of PHP are covered in this tutorial to help define what PHP is and how it can be used to develop professional web pages.

What to Expect

As you traverse the links in this tutorial your progress is indicated at the top of the page. There will be a 10 question quiz to test your knowledge of the material covered with results of your score. The quiz is a guide to help you absorb the concepts and syntax of PHP.

PHP is a powerful tool for making dynamic and interactive Web pages. It is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

In The Basics of PHP tutorial you will learn about PHP, and how to write and execute scripts on your web server.

What you should know

The goal of this tutorial would be to demonstrate the ease of use and simplicity in developing web sites and applications with PHP as a viable server side web technology. In this tutorial we will cover some basic code snippets and example usage of the code in action. We will go over how it interacts with the web server and touch upon the concepts and methodology of using PHP in the real world for web applications and web sites.  This tutorial will cover syntax, variables, functions and output.

The basic concepts, syntax and uses of PHP are covered in this tutorial to help define what PHP is and how it can be used to develop professional web pages.

What to Expect

As you traverse the links in this tutorial your progress is indicated at the top of the page. There will be a 10 question quiz to test your knowledge of the material covered with results of your score. The quiz is a guide to help you absorb the concepts and syntax of PHP.

What is PHP and its output

What is PHP?

  • PHP stands for "Hypertext Preprocessor", it is a server-side scripting language, like ASP
  • PHP scripts are executed on a web server (e.g. Apache or IIS)
  • PHP supports many databases (MySQL, MSSQL, Oracle, Sybase, Generic ODBC, etc.)
  • PHP is categorized as an open source software and is free to download and use
  • PHP is similar to other languages such as Perl and C

What is a PHP File?

  • It can contain text, HTML tags and scripts
  • PHP renders HTML in the browser
  • They have a file extension of ".php", ".php3", or ".phtml"
  • You can also include blocks of code via other php files with .inc extensions.

<?php

// this is what a php file looks like, the page name is "example.php"


	echo "This is a <i>PHP</i> file";

?>

Result: This is a PHP file

What is MySQL?

  • It is a database server
  • An ideal solution for both small and large applications
  • Supports standard SQL syntax

Why PHP?

  • Platform Support, PHP runs on different platforms (Windows, Linux, Unix, etc.)
  • Free, You can download PHP for FREE from the official PHP resource: www.php.net
  • Compatibility, PHP is compatible with almost all web servers and computers used today (Mac, WIndows, Apache, IIS, etc.)
  • Easy to learn, PHP is an easy language to learn and there is a vast amount of resources on the web to help assist you should you need more resources.

PHP Files, Database & Server

The diagram below shows the process from a user sending a request to the web server, how PHP makes a call to the database, and gets a response to render back to the users browser. Click the arrow to walk through each phase of the process.

What is required to utilize PHP

You'll need to get access to a web server with PHP support, below are some starting points:

  • Install Apache (or IIS) on your own server/computer, install PHP, and MySQL (optional)
  • Find a web hosting plan with PHP and optional MySQL support

Install PHP

What do you Need?

If your server supports PHP you don't need to do anything. Just create some .php files in your web directory, and the server will parse them for you. Because it is free, most web hosts offer PHP support. However, if your server does not support PHP, you must install PHP. Here is a link to a good tutorial from PHP.net on how to install PHP5: http://www.php.net/manual/en/install.php

Lessons

The goal of this tutorial would be to demonstrate the ease of use and simplicity in developing web sites and applications with PHP as a viable server side web technology. In this tutorial we will cover some basic code snippets and example usage of the code in action. We will go over how it interacts with the web server and touch upon the concepts and methodology of using PHP in the real world for web applications and web sites.  This tutorial will cover syntax, variables, functions and output.

The basic concepts, syntax and uses of PHP are covered in this tutorial to help define what PHP is and how it can be used to develop professional web pages.

What to Expect

As you traverse the links in this tutorial your progress is indicated at the top of the page. There will be a 10 question quiz to test your knowledge of the material covered with results of your score. The quiz is a guide to help you absorb the concepts and syntax of PHP.

PHP is a powerful tool for making dynamic and interactive Web pages. It is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

In The Basics of PHP tutorial you will learn about PHP, and how to write and execute scripts on your web server.

Syntax

PHP code is executed on the server, and the plain HTML result is sent to the browser.

Basic Syntax

A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document. On servers with shorthand support enabled you can start a scripting block with <? and end with ?>. It is recommended that you use the standard form (<?php) rather than the shorthand form. In PHP you can use both double quotes or single quotes to wrap strings and set variables.

<?php

// this is a single line comment in PHP

/* 
this is a multi-line PHP comment
you can add as many lines as you need
*/

?>

A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. Below, is an example of a simple PHP script which sends the text "Hello World" to the browser:

<?php

<html>
<body>
<?php
   echo "Hello World";
?>
</body>
</html>

?>

Variables

A variable is used to store information.

Variables in PHP

Variables are used for storing values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a $ sign symbol.

The correct way of declaring a variable in PHP:

<?php

$var_name = value;

?>

New PHP programmers often forget the $ sign at the beginning of the variable. In that case it will not work. Let's try creating a variable containing a string, and a variable containing a number:

<?php

$str_var="Hello World!";
$x=16;
			
?>

Naming Rules for Variables

  • A variable name must start with a letter or an underscore "_"
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)

Strings

String Variables in PHP

String variables are values you can set that are made of characters.

If you create a string variable in PHP you can change it, reuse it, or add to it. You can use a string directly in a function or the string can be stored in a variable.

In this example, the PHP script assigns the text "Hello World" to a string variable called $str_var:

<?php
$str_var = "Hello World";
echo $str_var;
?>

The output of the code above will be:

Hello World

The Concatenation Operator

PHP only has one string operator, it is the (.) (period) you would use it to put two string values together.

Below is how to concatenate two string variables together:

<?php
$str_var1 = "Hello World!";
$str_var2 = "This is PHP.";
echo $str_var1 . " " . $str_var2;
?>

The output of the code above will be:

Hello World! This is PHP.

Notice that we used the concatenation operator twice. This is because we had to insert a third string (a space character), to separate the two strings.

Simple Arrays

What is an Array?

An array is a type of variable that can store multiple values in one single variable. It is more like a categorized list. Arrays automatically start at 0 by default unless set on purpose.

If you have a list of items such as pet names, storing the pets in a single variable could look like this:

<?php
$pet[0] = "Skippy";
$pet[1] = "Tessie";
$pet[2] = "Lucky";
?>

This type of array is called a Numeric Array. There are three types of arrays in PHP:

  • Numeric Array
  • Associative Array
  • Multidimensional Array

In the example above you would access "Tessie" like this:

<?php

echo "My dog is: " . $pet[1];

?>

The above would output:

My dog is: Tessie

You can use a function called print_r($pet); to print out every set value in the array. It is like echo but just dumps all the set values of the array.

<?php
print_r($pet);
?>

Would look like this in your browser:

Array
(

[0] =>Skippy
[1] =>Tessie
[2] =>Lucky

)

For more information on Arrays visit: http://www.php.net/manual/en/language.types.array.php

Basic Functions

Next we will learn the basics of creating your own custom PHP function. A good reason we create custom functions is to keep the script from being executed when the page loads. A function is executed when a call to the function is requested by the user or another action. Code wise, you may call a function from anywhere within a page which allows it to be versatile and efficient.

Make your own function

<?php

function yourFunction(){
	code to be executed goes in open and close curly braces;
}

?>

Below is a simple function to call a color:

<html>
<body>

<?php

function writeColor(){
	echo "Orange";
}

echo "The color is: ";
writeColor();

?>

</body>
</html>

The output of the code above will be:

The color is: Orange



Arguments

Functions can be utilized more dynamically by adding arguments to the function. If we want a function to return different results based on the argument passed to it we can do this easily like this:

<html>
<body>

<?php

function writeColor($color){
	echo $color;
}

echo "The color is: ";
writeColor("Orange");

?>

</body>
</html>

The output of the code above will be:

The color is: Orange



PHP Built-in Functions

Built in functions are functions that are built in to PHP and are used simply by calling them. Lets look at a simple built in function for a date. The built in function is date(); it takes arguments you specify like the Year is "Y"

<?php

	echo "The year is: " . date("Y");
	
?>

The above example would output:

The year is: 2012

For a complete reference library of all PHP Functions visit php.net function references.

$_POST, $_REQUEST, and $_GET Functions

The $_POST Function

$_POST is a built in function that is used to collect values in a submitted form with method="post".

Values sent from a form with the POST method are invisible to people and has no limits on the amount of information to send. You can change the ceiling limits in your php.ini configuration file on the server. The best time to use the $_POST function is when you do not want to reveal any key or values in the browsers address bar.

<html>
<body>		
<form action="submit.php" method="post">
Name: <input type="text" name="first_name" />
Age: <input type="text" name="your_age" />
<input type="submit" />
</form>
</body>
</html>

When the "Submit" button is clicked, the URL will not change it will look like this:

http://www.example.com/submit.php

The "submit.php" file can now use the $_POST function to call form data variables that were set (the names of the form fields will be the keys in the $_POST array) the value is the result of the function:

Welcome <?php echo $_POST["first_name"]; ?>!
You are <?php echo $_POST["your_age"]; ?> years old.

 

The $_REQUEST Function

The PHP built-in $_REQUEST function contains the contents of both $_GET, $_POST, and $_COOKIE.

The $_REQUEST function can be used to collect form data sent with both the GET and POST methods.

<html>
<body>		
<a href='submit.php?first_name=Paul&your_age=38'>Click for Name and Age</a>
</body>
</html>

When the link is clicked, the URL will change to this:

http://www.example.com/submit.php?first_name=Paul&your_age=38

It sends the values in the address bar as a query string to "submit.php", you can then call the values like this:

<?php

	echo "Your name is: " . $_REQUEST['first_name'];
	echo "<br />";
	echo "Your age is: " . $_REQUEST['your_age'];

?>

 

The $_GET Function

Similar to the $_POST function, the difference is the method in the form needs to be "method='get'". The form keys and values are displayed as a visible query string in the browsers address bar.

<html>
<body>		
<form action="submit.php" method="get">
Name: <input type="text" name="first_name" />
Age: <input type="text" name="your_age" />
<input type="submit" />
</form>
</body>
</html>

When the "Submit" button is clicked, the URL will change, it will look like this:

http://www.example.com/submit.php?first_name=Paul&your_age=38

PHP Quiz

Test your PHP skills, now that you have gone through the tutorial see how many questions you can answer correctly. The test is not official, it's just a nice way to see how much you know, or don't know, about PHP. Good Luck!

The Quiz

The quiz contains 10 questions and there is no time limit.

Score

You will get 10 points for each correct answer. At the end of the Quiz, your total score will be displayed. Maximum score is 100 points.

What does PHP stand for?

  • Pre-hypertext Processor
  • Private Home Page
  • Hypertext Preprocessor
  • Preprocessor Hypertext Protocol

PHP server scripts are surrounded by delimiters, which?

  • <script>...</script>
  • <&>...</&>
  • <php>...</php>
  • <?php ... ?>

How do you write "Hello World" in PHP?

  • echo "Hello World";
  • Document.write(Hello World!)
  • "Hello World!";

All variables in PHP start with which symbol?

  • #
  • ?
  • &
  • $

What is the correct way to end a PHP statement?

  • </>
  • ;
  • .
  • end

The PHP syntax is most similar to:

  • Perl and C
  • JavaScript
  • ASP
  • JSP

How do you get information from a form that is submitted using the "get" method?

  • $_POST
  • $_GET
  • $_SERVER
  • GET

When using the POST method, variables are displayed in the URL:

  • True
  • False

In PHP you can use both single quotes ( ' ' ) and double quotes ( " " ) for strings:

  • False
  • True

Include files must have the file extension ".inc"

  • True
  • False

Results

Your Results

Below is the results of your quiz:

Resources

PHP.net

Full documentation and syntax: http://www.php.net
Function List Alphabetical: http://www.php.net/manual/en/extensions.alphabetical.php