0% found this document useful (0 votes)
62 views

PHP Tutorial

The document provides an introduction to PHP including what PHP is, how PHP files work, what PHP can do, why use PHP, and new features in PHP 7. It discusses that PHP is a scripting language used to make dynamic web pages, is free and efficient. It also provides information on installing PHP and the basic syntax of PHP scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

PHP Tutorial

The document provides an introduction to PHP including what PHP is, how PHP files work, what PHP can do, why use PHP, and new features in PHP 7. It discusses that PHP is a scripting language used to make dynamic web pages, is free and efficient. It also provides information on installing PHP and the basic syntax of PHP scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PHP 

Tutorial
❮ HomeNext ❯

Learn PHP
PHP is a server scripting language, and a powerful tool for making dynamic
and interactive Web pages.

PHP is a widely-used, free, and efficient alternative to competitors such as


Microsoft's ASP.

Start learning PHP now »

Easy Learning with "PHP Tryit"


With our online "PHP Tryit" editor, you can edit the PHP code, and click on a
button to view the result.

ExampleGet your own PHP Server


<!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>

Try it Yourself »

Click on the "Try it Yourself" button to see how it works.

PHP Exercises
Test Yourself With Exercises
Exercise:
Insert the missing part of the code below to output "Hello World".

"Hello World";

PHP Examples
Learn by examples! This tutorial supplements all explanations with clarifying
examples.

See All PHP Examples

PHP Quiz Test


Learn by taking a quiz! This quiz will give you a signal of how much you
know, or do not know, about PHP.

Start PHP Quiz!

My Learning
Track your progress with the free "My Learning" program here at W3Schools.

Log in to your account, and start earning points!

This is an optional feature. You can study W3Schools without using My


Learning.

PHP References
W3Schools' PHP reference contains different categories of all PHP functions,
keywords and constants, along with examples.

PHP References
W3Schools' PHP reference contains different categories of all PHP functions, keywords and
constants, along with examples.

Array Calendar Date Directory Error Exception Filesystem Filter FTP JSON Keywords Libx
ml Mail Math Misc MySQLi Network Output RegEx SimpleXML Stream String Var
Handling XML Parser Zip Timezones

Kickstart your career


Get certified by
completing the PHP course
Get certifiedw3sc
H

PHP Introduction
❮ PreviousNext ❯

PHP code is executed on the server.

What You Should Already Know


Before you continue you should have a basic understanding of the following:

 HTML
 CSS
 JavaScript

If you want to study these subjects first, find the tutorials on our Home page.
What is PHP?
 PHP is an acronym for "PHP: Hypertext Preprocessor"
 PHP is a widely-used, open source scripting language
 PHP scripts are executed on the server
 PHP is free to download and use

PHP is an amazing and popular language!

It is powerful enough to be at the core of the biggest blogging system on the


web (WordPress)!
It is deep enough to run large social networks!
It is also easy enough to be a beginner's first server side language!

What is a PHP File?


 PHP files can contain text, HTML, CSS, JavaScript, and PHP code
 PHP code is executed on the server, and the result is returned to the
browser as plain HTML
 PHP files have extension ".php"

What Can PHP Do?


 PHP can generate dynamic page content
 PHP can create, open, read, write, delete, and close files on the server
 PHP can collect form data
 PHP can send and receive cookies
 PHP can add, delete, modify data in your database
 PHP can be used to control user-access
 PHP can encrypt data

With PHP you are not limited to output HTML. You can output images or PDF
files. You can also output any text, such as XHTML and XML.

Why PHP?
 PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
 PHP supports a wide range of databases
 PHP is free. Download it from the official PHP resource: www.php.net
 PHP is easy to learn and runs efficiently on the server side

What's new in PHP 7


 PHP 7 is much faster than the previous popular stable release (PHP
5.6)
 PHP 7 has improved Error Handling
 PHP 7 supports stricter Type Declarations for function arguments
 PHP 7 supports new operators (like the spaceship operator: <=>)

❮ PreviousNext ❯

COLOR PICKER

PHP Installation
❮ PreviousNext ❯

What Do I Need?
To start using PHP, you can:

 Find a web host with PHP and MySQL support


 Install a web server on your own PC, and then install PHP and MySQL
Use a Web Host With PHP Support
If your server has activated support for PHP you do not need to do anything.

Just create some .php files, place them in your web directory, and the server
will automatically parse them for you.

You do not need to compile anything or install any extra tools.

Because PHP is free, most web hosts offer PHP support.

Set Up PHP on Your Own PC


However, if your server does not support PHP, you must:

 install a web server


 install PHP
 install a database, such as MySQL

The official PHP website (PHP.net) has installation instructions for


PHP: http://php.net/manual/en/install.php

PHP Online Compiler / Editor


With w3schools' online PHP compiler, you can edit PHP code, and view the
result in your browser.

Run »
<?php
$txt = "PHP";
echo "I love $txt!";
?>
I love PHP!

Try it Yourself »

Click on the "Try it Yourself" button to see how it works.


PHP Syntax
❮ PreviousNext ❯

A PHP script is executed on the server, and the plain HTML result is sent
back to the browser.

Basic PHP Syntax


A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>:

<?php
// PHP code goes here
?>

The default file extension for PHP files is ".php".

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses
a built-in PHP function "echo" to output the text "Hello World!" on a web
page:

ExampleGet your own PHP Server


<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

Try it Yourself »
Note: PHP statements end with a semicolon (;).

ADVERTISEMENT

PHP Case Sensitivity


In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and
user-defined functions are not case-sensitive.

In the example below, all three echo statements below are equal and legal:

Example
<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body>
</html>

Try it Yourself »
Note: However; all variable names are case-sensitive!

Look at the example below; only the first statement will display the value of
the $color variable! This is because $color, $COLOR, and $coLOR are treated as
three different variables:

Example
<!DOCTYPE html>
<html>
<body>

<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>

</body>
</html>

Try it Yourself »

PHP Exercises
Test Yourself With Exercises
Exercise:
Insert the missing part of the code below to output "Hello World".

"Hello World";

You might also like