Skip to content

Getting Started with PHP Coding

Let’s create a file named index.php in the htdocs folder.

You can open the file we created with a code editor.

How to Write PHP Code?

PHP code can be embedded into HTML pages. A PHP code block starts with <?php and ends with ?>. For example:

<!DOCTYPE html>
<title>Hello PHP</title>
<h1><?php echo "Hello, World!"; ?></h1>

After writing this code, you can view the output by visiting localhost or

Congratulations, in this section, you used the echo statement between HTML tags to display the text Hello World on the screen.

Variables and Data Types::

In PHP, variables start with $ (dollar) sign. For example:

$ad = "Hamza";
$yas = 25;

In this example, $name is a string, and $age is an integer. PHP automatically determines the variable’s data type.

Loops and Conditions:

PHP includes control structures like if, else, while, for . For example:

$number = 10;
if ($number > 0) {
echo "Number is positive.";
} elseif ($number < 0) {
echo "Number is negative.";
} else {
echo "Number is zero.";
// Loop
for ($i = 0; $i < 5; $i++) {
echo $i;

This example includes an if-else structure and a for loop.


Functions can be defined and used in PHP. For example:

function add($num1, $num2) {
return $num1 + $num2;
$result = add(5, 3);
echo "Sum: " . $result;

In this example, a function named add is defined, and this function is used to perform an addition operation.

Database Operations:

PHP can interact with databases like MySQL. It can perform operations such as database connection, queries, and result processing.

These fundamental topics are useful to get started with PHP. To learn more advanced concepts and applications of PHP, you can explore various resources and practice on projects.