Click to get information about our services.
PHP is a server-side programming language and is used to create dynamic websites. PHP codes are written in files with .php extension, and these files are interpreted by the web server and sent to the client as HTML, CSS and JavaScript codes. In this article, we will cover how you can create PHP files and basic examples.
Creating a PHP File
PHP files can be created using a text editor or code editor. First, open a text editor or code editor and create a new file. The file name must end with the ".php" extension. For example, "index.php".
To start writing PHP codes, add the command "<?php" to the first line when the file is opened. This command marks the beginning of PHP codes. The "?>" command is used to terminate the codes.
<?php
// PHP codes are written here
?>
PHP codes are written between these start and end commands. For example, the "echo" command is used to output a text or value.
<?php
echo "Hello World!";
?>
In this example, "Hello World!" with the "echo" command. The text is printed on the screen.
Running PHP Codes
For PHP codes to work, the file must be interpreted by a web server. Therefore, you first need to install a web server. The most commonly used web servers include Apache, Nginx, IIS, etc. is located.
PHP codes can be viewed in the web browser after being uploaded to the web server. It can also be accessed through a web application running on the server.
PHP File Examples
Below are basic PHP examples.
1. Finding the Sum of Two Numbers:
<?php
$a = 5;
$b = 10;
$c = $a + $b;
echo "Toplam: " . $c;
?>
In this example, variables $a and $b are defined and their sums are assigned to variable $c and printed on the screen.
2. Receiving Data from the User:
<?php
$ad = $_POST['ad'];
$soyad = $_POST['soyad'];
echo "Merhaba " . $ad . " " . $soyad;
?>
In this example, using an HTML form, the user's name and surname information is obtained and printed on the screen.
3. Factorial Calculation:
<?php
function faktoriyel($n) {
if ($n == 0) {
return 1;
} else {
return $n * faktoriyel($n-1);
}
}
echo faktoriyel(5); // 120
?>
In this example, the factorial calculation is performed with the help of a function. The number to be calculated ($n) is given as a parameter to the function and the result is returned.
Result:
PHP is a language that everyone interested in web development should learn. Having knowledge on this subject allows you to create dynamic and interesting websites. In this article, we have covered how you can create PHP files and basic examples. We hope that by applying this information, it will help you learn PHP.