PHP

Server-side scripting language used to build dynamic web applications

Lessons
  • PHP Introduction
  • PHP Installation
  • Introduction to PHP
  • Variables and Data Types
  • Conditional Statements
  • Loops in PHP
  • Arrays
  • Functions
  • Forms and $_GET / $_POST
  • File Handling
  • Sessions and Cookies
  • Connecting to a MySQL Database (with mysqli)
PHP Introduction

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: <=>)

--------------------------------------------------------------------------------

Example:
-

Example Description:

-
PHP Installation

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 Version
To check your php version you can use the phpversion() function:

Example:
echo phpversion();

Example Description:

Display the PHP version:
Introduction to PHP

PHP is a server-side scripting language used to create dynamic web pages and interact with databases.

Example:
<?php echo "Hello, PHP!"; ?>

Example Description:

This code uses echo to print "Hello, PHP!" to the browser. It’s the simplest way to output data in PHP.
Variables and Data Types

PHP variables start with $ and can hold strings, integers, floats, booleans, arrays, and objects

Example:
<?php $name = "Ali"; $age = 25; echo "My name is $name and I am $age years old."; ?>

Example Description:

This script declares two variables and displays them inside a string using double quotes.
Conditional Statements

PHP supports if, else, elseif, and switch statements for controlling logic flow

Example:
<?php $score = 85; if ($score >= 90) { echo "Excellent"; } elseif ($score >= 70) { echo "Good"; } else { echo "Needs Improvement"; } ?>

Example Description:

Depending on the $score, this code prints different messages using if-elseif-else statements.
Loops in PHP

Loops allow repeated execution of code using for, while, do-while, and foreach.

Example:
<?php for ($i = 1; $i <= 5; $i++) { echo "Number: $i<br>"; } ?>

Example Description:

This loop prints numbers 1 through 5 using a for loop.
Arrays

Arrays are used to store multiple values in one variable.

Example:
<?php $fruits = ["Apple", "Banana", "Orange"]; echo $fruits[1]; // Outputs: Banana ?>

Example Description:

This array holds three fruits. $fruits[1] accesses the second element ("Banana").
Functions

Functions in PHP allow you to group reusable code blocks.

Example:
<?php function greet($name) { return "Hello, $name!"; } echo greet("Sara"); ?>

Example Description:

The function greet takes a name and returns a greeting message.
Forms and $_GET / $_POST

PHP can collect data from forms using $_GET (URL) and $_POST (securely).

Example:
(HTML + PHP): <form method="post"> Name: <input type="text" name="username"> <input type="submit"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { echo "Hello, " . $_POST["username"]; } ?>

Example Description:

This form sends data to the same page using POST, and PHP retrieves the username and prints it.
File Handling

PHP allows reading/writing files using functions like fopen, fwrite, fread, and fclose.

Example:
<?php $file = fopen("sample.txt", "w"); fwrite($file, "Hello File!"); fclose($file); ?>

Example Description:

This code creates or overwrites sample.txt and writes "Hello File!" into it.
Sessions and Cookies

Sessions store user data across multiple pages. Cookies store data on the user's browser.

Example:
<?php session_start(); $_SESSION["user"] = "Admin"; echo $_SESSION["user"]; ?>

Example Description:

This starts a session and stores the username "Admin" in a session variable.
Connecting to a MySQL Database (with mysqli)

PHP connects to databases using mysqli or PDO.

Example:
<?php $conn = mysqli_connect("localhost", "root", "", "test_db"); if ($conn) { echo "Connected successfully!"; } ?>

Example Description:

This connects to a MySQL database named test_db on the local server using mysqli_connec
You have completed the lesson . Add To Your Completed Course

You have a test for this course. Do you want to submit it to pass the course?