PHP: Personal Home Page
Tags: beginners, php, javascript
Published: 2023-12-11 00:22:47

PHP(Hypertext Preprocessor) is an open source, server side scripting language which has been one of the most popular web development tools since its inception. Created in 1993 by Danish-Canadian programmer Rasmus Lerdorf, its original implementation was for Rasmus personal home page (PHP) but was eventually changed to represent PHP Hypertext Preprocessor. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5eff814e2sjdk6xwxux.png)

PHP code is usually processed on a web server by a PHP interpreter and the results are sent to the client with HTML. Server side processing has the additional benefits of greater performance and more secure transfer of user data. With PHP making its request on the server side, this will also give access to server resources like access to the database. The standard PHP interpreter is powered by the Zend Engine and is a free software that is licensed and released through PHP. 

## Why learn PHP in 2023?
PHP continues to be one of the most relevant and widely used programming languages used with web development. Since PHP can be embedded directly into HTML, it is easy to integrate server logic with front end code. It can support procedural and object-oriented programing styles. PHP has numerous frameworks like Laravel, Symfony and Codelgniter which can simplify the development of web applications. Content Management Systems (CMS), like WordPress are built with PHP. Other examples like Facebook, Yahoo, Tumblr and Wikipedia(along with 65% of the worlds top 1 million websites) are built using PHP. As of 2015, PHP is one of the used languages on the web (most of that coming from WordPress which runs 25% of internet sites).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30ln5bfrheewwzdubltu.png)

## Variables
PHP variables are containers created to store information and can support various data types such as strings, integers, floating point numbers, Boolean, arrays, objects, NULL and resource. Unlike other programming languages, PHP has no command for declaring a variable. Once you assign a value, the variable is created. PHP is a loosely typed language, meaning that we do not have to tell what the data type is when the variable is created (but the option to declare was introduced in PHP 7). PHP will automatically associate the data type to the variable. 

```
<?php
$name = "John";
$age = 25;

echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
?>
```
In the example above, we show that the variable declaration must start with a $ and followed by the name of the variable. A variable must start with a letter or underscore and cannot start with a number. Variables are case sensitive so in the example above $age and $AGE would be two different variables. 

## Conditional statements and loops
Conditional statements allow us to control the flow of code execution based off different conditions. An if statement will execute if one condition is true and an else block will run if that statement executes to false. The addition of an else if is another conditional check after the initial if has executed to false. Switch statements allow us to to compare a value against multiple possible values and execute different code blocks accordingly. 

```
<?php
$isLoggedOn = true;

if ($isLoggedON) {
   echo "Welcome, user!";
} else {
   echo "Please log in.";
}
?>

<?php
$grade = 85;

if ($grade >= 90) {
   echo "A";
} else if ($grade >= 80) {
   echo "B";
} else {
   echo "C";
}
?>

<?php
$dayOfWeek = "Monday";

switch ($dayOfWeek) {
   case "Monday":
      echo "It's the start of the week.";
      break;
   case "Thursday":
      echo "Its almost the weekend!";
      break;
   default:
      echo "It's just a regular day.";
}
?>

```

Loops allow us to run the same block of code a given number of times. Instead of writing one line to be ran multiple times, we can use loops to run a specific amount along as a certain condition is true. For loops are used when we know how many times we need to run and a while loop will continue to execute as long as condition is true. Do-while loops are the same as while but the code block will be executed at least once even if the starting condition is false. 

```
<?php
for ($i = 1; $i <= 5; $i++) {
   echo "Iteration $i <br>";
}
?>

<?php
$count = 1;
while ($count <= 3) {
   echo "Count: $count <br>";
   $count++;
}
?>

<?php
$counter = 1;
do {
   echo "Counter: $counter <br>";
   $counter++;
} while ($counter <= 3);
?>
```
Take note that we still need to include the $ in our loops as we are declaring a variable to be used for counting. 

## Functions
PHP has numerous built in functions that allow us create reusable blocks of code to perform a specific task. A function is a block of statements that can be used repeatedly within a program. Functions must be declared using the function keyword and need to be executed in order to run. The code needed to run needs to be enclosed within curly braces. We can include parameters(set as default or declared elsewhere) with our functions that hold variables to be passed within. They can return a value or echo a statement.  

```
<?php
//Function with parameters
function greetUser($name) {
   echo "Hello, $name!";
}

greetUser("John");
?>

<?php
//Function with default parameter value
function greetWithDefault($name = "Guest") {
   echo "Hello, $name!";
}

greetWithDefault(); 
?>

<?php
  function sum($arr) {
     return array_sum($arr);
    }
      $numbers = array(1, 2, 3, 4);
    echo sum($numbers);
 ?>

```
In the last code example we show the creation, use of an array and the use of a built in PHP function in array_sum. In order to create the array, we must use the array keyword and within our function, we declare the array_sum built in function. 

## Integrating PHP with HTML
PHP can be embedded within HTML files using specific tags. This will cause the page when rendered to send a request to the server which will process the PHP code, execute it and send the resulting HTML back to the client. 

```
<!DOCTYPE html>
<html>
<body>

<h1><?php echo "Hello, World!"; ?></h1>
<?php
$color = "blue";
$size = "large";
echo "The {$size} {$color} balloon is floating.";
?>

</body>
</html>
```
In the code snippet above, we are using the echo statement to display 'Hello World!' on a basic page. The 'echo' statement is used to output our content, typically HTML, to the browser. Echo has no return value but can take multiple parameters which we show in the below code example. 

```
<?php
$pageTitle = "My Website";
$content = "<p>Welcome to my site!</p>";

$stylesheet = "styles.css";
$javascript = "script.js";

echo "<link rel='stylesheet' href='$stylesheet'>";
echo "<script src='$javascript'></script>";

echo "<!DOCTYPE html>
      <html>
      <head>
         <title>$pageTitle</title>
      </head>
      <body>
         <h1>$pageTitle</h1>
         $content
      </body>
      </html>";
?>
```

PHP has the ability to directly output HTML tags which allows developers to generate HTML content based off code logic. This includes CSS stylesheets and JavaScript files. Adding these into the PHP tags allows for dynamic control over the loaded assets by the page. This can be fundamental to building dynamic and data driven web applications where the structure and content of the web page can change. 
 
In conclusion, PHP has been at the forefront of web development since its creation in 1993. Originally created for personal home page development, it has evolved into a powerful server side scripting language. Even today in 2023 its impact is seen across the web due to its integration with front end tech and support for procedural and object-oriented programming. 

Sources: 
https://www.w3schools.com/php/default.asp
https://4geeksacademy.com/us/trends-and-tech/4geeks-academy-teaches-php-backend-language
https://en.wikipedia.org/wiki/PHP