PHP stands for “Hypertext Preprocessor”. Php is an open source scripting language that is widely used to develop web applications. It is embedded into HTML and it interacts with databases to create dynamic functionality. PHP can pretty easily be installed, configured and is compatible with various platforms and that makes it very useful in developing dynamic and interactive web applications.
What you need to start working with PHP
- A web server (such as Apache or Nginx)
- A PHP installation (version 7.4 or above is recommended)
- A text editor to write code (e.g., Eclipse, VS Code, Sublime Text)
Basic PHP Syntax
PHP code is embedded into HTML using special PHP tags like this:
<?php
// PHP code goes here
?>
You can also use <?=
to output values:
<?= "Hello, World!" ?>
Example 1: Displaying “Hello, World!”
<?php
echo "Hello, World!";
?>
In this example, echo
the PHP code will display text or variables.
Example 2: Variables and Data Types
<?php
$name = "John Doe"; // String
$age = 30; // Integer
$height = 5.9; // Float
$isStudent = true; // Boolean
echo "Name: $name, Age: $age, Height: $height, Student: " . ($isStudent ? 'Yes' : 'No');
?>
This example shows how to define variables and it then prints them using echo
.
Example 3: Arrays in PHP
<?php
$fruits = array("Apple", "Banana", "Orange");
echo "I like " . $fruits[0] . ", " . $fruits[1] . ", and " . $fruits[2];
?>
This is an example of a basic indexed array. You can access array elements using their index (starting from 0).
Example 4: Conditional Statements
<?php
$score = 85;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 80) {
echo "Grade: B";
} else {
echo "Grade: C";
}
?>
This example checks the score and assigns a grade based on the “if” conditions.
Example 5: Loops in PHP
Loops in PHP are used to execute the same block of code multiple times as long as a condition is met. PHP supports for
, while
, and foreach
loops.
For Loop Example:
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br>";
}
?>
This loop prints numbers from 1 to 5.
Foreach Loop Example:
<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
echo "Color: $color <br>";
}
?>
The foreach
loop iterates over each value in the array.
Example 6: Functions in PHP
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Alice");
?>
In this example, the greet
function takes a name as input and then returns a greeting message.
Example 7: Working with Forms
PHP is often used to process form data and then user can sue that data accordingly as per the requirement. Below is an example of a simple form and PHP script to handle the form data.
HTML Form:
<form method="POST" action="process.php">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
PHP Form Handler (process.php
):
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
echo "Hello, $name!";
}
?>
This example captures the name entered in the form, sanitizes it using htmlspecialchars()
, and then displays when the form is submitted.
In this article, I have explained what is PHP, what you need to use to start working with PHP, PHP syntax and some basic examples of PHP, including how to print text, use variables, arrays, loops, and conditional statements, define functions, and handle form data. As you continue to learn, you’ll discover the vast capabilities of PHP, such as interacting with databases and working with APIs.