Overview of PHP
What is PHP?
PHP, which stands for "Hypertext Preprocessor," is a widely-used open source general-purpose scripting language. It is especially suited for web development and can be embedded into HTML. PHP scripts are executed on the server, and the result is returned to the browser as plain HTML.
History of PHP
PHP was created by Rasmus Lerdorf in 1994. Initially, it was a set of Common Gateway Interface (CGI) binaries written in the C programming language. Over time, it has evolved into a full-fledged programming language with the help of contributions from developers around the world.
Why Use PHP?
PHP has several benefits that make it a popular choice for web development:
- Easy to learn and use
- Open-source and free
- Cross-platform compatibility
- Supports a wide range of databases
- Large community and plenty of resources available
Basic Syntax of PHP
PHP scripts are enclosed within <?php ... ?>
tags. A PHP script can be placed anywhere in the document. PHP files have a default file extension of .php
.
<?php
echo "Hello, World!";
?>
Embedding PHP in HTML
PHP can be easily embedded within HTML to create dynamic web pages. Here is a simple example:
<html>
<body>
<h1>My First PHP Page</h1>
<?php
echo "Hello, World!";
?>
</body>
</html>
Variables in PHP
Variables in PHP are represented by a dollar sign followed by the name of the variable. PHP variable names are case-sensitive.
<?php
$txt = "Hello, World!";
echo $txt;
?>
PHP Data Types
PHP supports several data types, including:
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
Conditional Statements in PHP
PHP supports the following conditional statements:
if
statementif...else
statementif...elseif...else
statementswitch
statement
Example of an if
statement:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
Loops in PHP
PHP supports the following types of loops:
while
loopdo...while
loopfor
loopforeach
loop
Example of a for
loop:
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Functions in PHP
A function is a block of statements that can be used repeatedly in a program. Functions in PHP are declared with the function
keyword.
<?php
function writeMsg() {
echo "Hello, World!";
}
writeMsg(); // call the function
?>
Connecting to a Database using PHP
PHP can connect to various database systems such as MySQL, PostgreSQL, SQLite, etc. Here is an example of connecting to a MySQL database:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Conclusion
PHP is a powerful and flexible scripting language that is widely used for web development. Its ease of use, combined with its ability to interact with databases and handle dynamic content, makes it a popular choice among developers. This tutorial has provided an overview of PHP, its syntax, and some basic functionalities. To become proficient in PHP, continue exploring its advanced features and practice writing PHP scripts.