Simple Example of PHP Programming Language for Beginners Complete with Explanation
A Simple Example of the Most Popular Programming Language There are many types of programming languages that are widely used by programmers in Indonesia and even around the world. One of the most popular ones is PHP.
In this article, Mangcoding will discuss the basics of installation, writing, and PHP syntax. Without further ado, let’s get into the discussion.
PHP stands for Hypertext Preprocessor, a scripting language similar to JavaScript and Python.
The difference is that PHP is mainly used for server-side communication, whereas JavaScript can be used for both frontend and backend. Meanwhile, Python is exclusively for server-side (backend) development.
A scripting language automates the execution of tasks within a specific runtime environment. These tasks include instructing static pages (built with HTML and CSS) to perform certain actions based on predefined rules.
For example, we can use a script to validate whether all fields in a form are filled before it is submitted to the server. The script will run and check all fields when the user submits the form.
Before using PHP syntax, we must first install PHP. There are several ways to install PHP :
Download PHP from the official website (https://www.php.net/) and install it manually on your machine. (This method is not recommended because it is quite complicated.)
Use third-party applications such as XAMPP, WAMP, Laragon, and App Server.
- Xampp : https://www.apachefriends.org/download.html (Rekomendasi)
- Wamp : https://www.wampserver.com/en/
- Laragon : https://laragon.org/download/index.html
- App Serve : https://www.appserv.org/en/
PHP can be executed in two ways: via CLI (Command Line Interface) or using third-party applications.
1. Using CLI
If using CLI, we can type php -S address:port in our terminal, for example : php -S localhost:4000 Then, we can open the URL http://localhost:4000 in a browser.
[~/projects/learning > phph -S localhost:4000 21:06:19 ]
[Thu Nov 10 21:06:21 2022] PHP 8.1.11 Development Server (http://localhost:4000)
started
[Thu Nov 10 21:06:22 2022] 127.0.0.1:56804 Accepted
[Thu Nov 10 21:06:22 2022] 127.0.0.1:56804 [200]: GET /
[Thu Nov 10 21:06:22 2022] 127.0.0.1:56804 Closing
[Thu Nov 10 21:06:22 2022] 127.0.0.1:56806 Accepted

2. Using Third-Party Applications
Similar to the first method, but this time using a third-party application like XAMPP makes it easier. We just need to open the XAMPP application and click “Start” on the Apache module.

To run PHP, we need to create a document with a .php extension, for example, “coba.php”. If we are using XAMPP, we can save the file in : C:\Xampp\htdocs\file-name.php
PHP can be written inside or outside HTML tags. The PHP syntax must begin with , or if it’s just for displaying text, we can use . Example :
coba.php
<?php
echo "Hallo kawan kawan";
?>
<?= $word ?>
PHP is very sensitive to semicolons (;). If a semicolon is missing in the code, it will result in an error. Example:
coba.php
<?php
$word = "Hallo kawan kawan"
$word2 = "test"
?>
<?= $word ?>
1. Echo/Print/Printf
Echo
The echo() function is used to display text on the screen. This function can be used with or without parentheses. Example :
coba.php
<?php
echo "<h2>Belajar PHP itu mudah!</h2>";
echo("Hello world!<br>");
echo "Aku edang belajar PHP!<br>";
echo "Ini", "teks", "yang", "dibuat", "terpisah";
?>
The result will be :

The print() function is similar to the echo() function. It is also used to display text on the screen and can be used without parentheses. The differences between print() and echo() are :
- The print() function always returns a value of 1 when executed, whereas echo() does not return anything.
- The print() function can only take one parameter, while echo() can take multiple parameters.
Example :
coba.php
<?php
print "<h2>Belajar PHP</h2>";
print "Hello world!<br>";
print "Belajar mencetak teks di PHP!";
?>
The result will be :

PrintF
The printf() function is used to format text or strings. This function returns the length of the text when executed. There are several symbols that can be used, including :
- %s for strings
- %d for decimal numbers (integers)
- %f for floating-point numbers
- %b for boolean values
Example :
coba.php
<?php
$txt = "PHP";
printf("Aku belajar pemrograman PHP d %s<br>", $txt);
?>
The result will be :

That’s the explanation of A Simple Example of PHP Programming Language that Mangcoding can share. Hopefully, this article is useful and provides new insights for you. If you have constructive feedback or suggestions, feel free to leave a comment or contact us via email and Mangcoding’s social media.