What Is PHP?
PHP is a programming language widely used for server-side web development. It can generate web pages, process forms, communicate with databases, manage sessions and build dynamic websites and applications.
PHP is a server-side scripting language
PHP is a general-purpose programming language particularly suited to web development.
PHP code commonly runs on a web server rather than directly inside the user's browser.
The server executes the PHP code and sends the resulting output, often HTML, back to the browser.
What does PHP stand for?
PHP is officially described using the recursive acronym:
The name reflects PHP's long association with generating and processing web content.
What does PHP look like?
The echo statement sends text to the output.
In a web application, that output can become part of the HTML returned to the browser.
PHP usually runs before the page reaches the browser
The browser usually receives the output generated by PHP rather than the original PHP source code.
PHP can be embedded inside HTML
PHP executes on the server and inserts the generated value into the document before it is sent to the user.
PHP can store information in variables
PHP variable names normally begin with a $ symbol.
PHP supports different kinds of data
| Type | Example |
|---|---|
| String | "Freelance Coder" |
| Integer | 42 |
| Float | 19.99 |
| Boolean | true |
| Array | ["HTML", "CSS", "PHP"] |
| Null | null |
| Object | new Customer() |
PHP arrays can store collections of data
This outputs:
PHP arrays can also use named keys
Associative arrays are useful for representing structured information using named keys and values.
PHP can make decisions
Conditional statements allow different code to run depending on the state of the application.
PHP functions package reusable logic
The full stop operator joins strings together in PHP.
PHP can repeat tasks
Loops are useful when the same operation must be repeated several times.
foreach is useful for collections
The loop processes each item in the array one at a time.
PHP can process form submissions
A website might contain a form:
PHP can receive the submitted data:
User input should be validated and handled safely. Real applications should never assume that submitted information is automatically trustworthy.
PHP can communicate with databases
PHP is commonly used with relational databases such as MySQL and PostgreSQL.
An application might use a database to store:
PHP provides database interfaces
One common approach is PHP Data Objects, or PDO.
Applications can then execute database queries through the connection.
Prepared statements are important
Prepared statements allow values to be supplied separately from the SQL structure and are an important defence against SQL injection.
PHP can maintain session data
Sessions allow a server to associate information with a user across multiple requests.
They are commonly used for login systems and other stateful website features.
PHP can also work with cookies
Cookies are stored by the browser and can later be sent back to the server with requests.
PHP works with JSON
PHP can convert arrays and objects into JSON:
It can also convert JSON into PHP data:
This makes PHP useful for building and consuming APIs.
PHP can power APIs
Instead of returning an HTML page, a PHP application can return structured data.
Another application can then consume the JSON response.
PHP and JavaScript often work together
PHP
Commonly runs on the server and handles server-side application logic.
JavaScript
Commonly runs in the browser and handles interactive client-side behaviour.
PHP Output
Can generate HTML or JSON that is sent to the browser.
JavaScript Requests
Can send requests to PHP-powered APIs and process their responses.
A PHP web application might work like this
PHP often acts as the server-side layer connecting users, application logic and stored data.
WordPress is built with PHP
PHP has played a major role in the development of content management systems.
WordPress uses PHP extensively for themes, plugins, templates, database interaction and server-side logic.
WordPress-specific PHP functions can dynamically retrieve and display website content.
PHP code is commonly stored in .php files
A web server configured for PHP can execute the PHP code when one of these files is requested.
Server-side code must handle data carefully
PHP applications often process passwords, form inputs, database queries, cookies and uploaded files.
Secure applications therefore need appropriate validation, escaping, authentication and database practices.
Never trust input simply because it came from your own website. Server-side code should validate data and apply the correct security controls for how that data will be used.
PHP has a large software ecosystem
Developers can use frameworks and content management systems rather than building every component from scratch.
What is PHP used for?
The server does the work.
PHP is a programming language widely used for server-side web development. It can generate HTML, process forms, manage sessions, communicate with databases, work with JSON and APIs, and power dynamic websites and applications such as WordPress.