Basics of PHP

Nicky Liu
The Startup
Published in
6 min readNov 23, 2019

This article will explain some of the basics of PHP and how to use it.

Create a folder.

php_test

CD into the folder and create a new file inside it named:

info.html

First set up a basic HTML page inside of info.html.

<html>
<head>
</head>
<body>
<h1>hi</h1>
<form action="php_test.php" method="post">
First Name:
<br />
<input type="text" name="firstname" />
<br />
Last Name:
<br />
<input type="text" name="lastname" />
<br />
<input type="submit" />
</form>
</body>
</html>

The action inside of form lets you link the page with a PHP file. The “post” method lets it know that we will pass in data through a form.

Now create a php file:

php_test.php

Inside of that file enter:

<html>
<head>
<title>PHP INFO</title>
</head>
<body>
<?php
echo "<p>PHP ECHO!!</>";
?>
</body>
</html>

A PHP file still follows html, but you embed PHP using:

<?php?>

Echo

The way you display a line is through the use of “echo”:

echo "<p>PHP ECHO!!</p>"

This will display the word “ECHO” in an HTML paragraph. You can also write it as:

echo "<p>PHP ECHO!!</>"

Make sure to end every line with a “;” or things will bug out as the lines will be fused together.

You can now open your file by running:

open index.html

You will get a page that says “hi” at the top, as well as 2 bars asking for your first and last name, and a submit button.

Enter anything into the bars and hit submit.

You should see a page that has your php code on it. This is not what you want.

This opens the files, but doesn’t actually run your PHPcode. So let’s fix that.

If you don’t already have PHP installed, install the latest version on this link: https://php-osx.liip.ch/.

Now run your files using:

php -S localhost:8000

Now on your browser, go to:

http://localhost:8000/file_name.htmlie: http://localhost:8000/info.html

Now if you enter some stuff and hit submit, the page will display the line “PHP ECHO!!!”

Success! You now have a site running both your html and your php file in the background!

Change the php file so that the line

echo "<p>PHP ECHO!!</>";

is repeated twice. If you refresh the page, they are appropriately spaced out. This is because they are separate paragraphs.

echo "<p>PHP ECHO!!</>";
echo "<p>PHP ECHO!!</>";

Change it so that instead you echo just the words in non paragraph form.

echo "PHP ECHO!!";

When you have just one line, it looks the same.

echo "PHP ECHO!!";
echo "PHP ECHO!!";

Now both lines are shown, but they are fused together next to each other.

You can space them out by using a break line.

echo "PHP ECHO!!" . "</br>";
echo "PHP ECHO!!" . "</br>";

The . in between allows you to join things together, similar to how you would normally use +. Now two separate “PHP ECHO!!” lines are displayed.

User Input/Variables

A PHP script is capable of taking in user input. In our info.html file we gave the two input lines names:

<input type="text" name="firstname" />

<input type="text" name="lastname" />

We will use these names to indicate the inputs given. The information can be accessed using:

$_POST['input_name']

For example, to get the information entered in the “firstname” bar, write:

echo $_POST['firstname'] . "</br>";

For the information in the “lastname” bar write:

echo $_POST['lastname'] . "</br>";

Enter whatever values you want for both bars, and you will see that they appear in two different lines, with the “firstname” value on top and the “lastname” value below.

Variables

PHP is also able to make use of variables. In the earlier section, instead of echoing the inputs for “firstname” and “lastname” directly, they can be set to a variable, and that variable be echo’ed instead.

A variable is set using:

$variableName = value;

To set two variables equal to the two inputs, you can use:

$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];

You will then change the echoes to:

echo $firstName . "</br>";
echo $lastName . "</br>";

This gives the same result as earlier if you enter some values and hit submit.

You can also make a long string by doing:

$variableName = <<<EOD
mumbo
jumbo
jambo
EOD;

Everything inside the EOD will count as one long continuous sentence/paragraph. Lines can be separated using </br>. Note that there cannot be any spaces before the second “EOD”.

For our above example if you add the following lines:

$str = <<<EOD
The customers name is
$firstName and they are
of the $lastName clan </br>
mumbo jumbo
EOD;
echo $str;

It will also print out the sentence:

The customers name is firstName and they are of the lastName clan.
mumbo jumbo

You can define a constant using:

define(constantName, constantValue)ie: define (highestScore, "100")

Add a new sentence:

echo "The highest possible score on a test is" . highestScore;

The page should now read:

"The highest possible score on a test is 100"

Math

PHP is also capable of doing math.

echo 5 + 2 . "</br>";
echo 5 / 2 . "</br>";

If you add this line, the screen prints out a 7 and a 2.5.

If you want the result as a certain data type you can add it in front using a (dataType ).

echo (integer)(5 / 2) . "</br>";

Here the result of 5/2, which is 2.5, is converted into an integer, so on your screen you should see a 2.

Loops

Loops can be used by inserting the echo statements after the conditions.

if(1 > 2){
echo '1 > 2' . "</br>";
} else {
echo '1 < 2' . "</br>";
}

The if statement will be checked and if a condition is met the echo code will be printed on the screen. Here you should see: 1 < 2

A case statement can similarly be used. Write the following code:

$firstName = $_POST['firstname'];switch($firstName){
case "first" :
echo "HELLO FIRST" . "</br>";
break;
case "durr" :
echo "HELLO DURR" . "</br>";
break;
default :
echo "hey my dude" . "</br>";
break;
}

Here we set the variable firstName equal to your input in the first box. If you enter “first” as your first name the screen will show “HELLO FIRST”. If you put “durr” as your first name the screen will show “HELLO DURR”. Otherwise if you enter anything else the screen will just show “hey my dude”.

You can use can put an echo inside of a while loop to have the screen show multiple lines, one for each time the loop runs.

$num = 0;
while($num < 10){
echo $num . "</br>";
++$num
}

This loop will print a line showing the value of num, then increment 1, and repeat until num is no longer smaller than 10. As a result the screen will show the numbers 0 through 9 each on a separate line.

Arrays

You can create an array by doing:

$varName = array(item1, item2, item 3);

You can replace an item in the array with:

$varName[index] = newItem

You can add to it instead by making the index bigger than any used.

Add the following lines.

$favoriteFoods = array('pizza', 'PIZZA', 'XL-PIZZA', 'XLSUPREMEEVERYTHINGONTOPPIZZA');$favoriteFoods[4] = 'diet salad';echo $favoriteFoods . "</br>";
echo "My favorite food is " . $favoriteFoods[0] . "</br>";
echo "My last favorite food is " . $favoriteFoods[4] . "</br>";

We create an array of our favorite foods, which may or may not be biased towards pizza. We add an extra ‘diet salad’ to the array. The first echo just prints out the data type: ‘array’. But the other two print out the first and last items in the array by using their indexes.

You can go through all of the items in the array and print them by doing:

forEach(arrayName as item){
echo $item;
}

If you copy down the following line:

forEach($favoriteFoods as $food){
echo $food . "</br>";
}

The screen will now also have a line for each food in the favoriteFoods array.

The word you use after “arrayName as” doesn’t actually matter, so long as you are consistent with using the word in the line afterwards. Obviously it helps to make it a meaningful word though, like an item from an array of foods would be one singular food.

These are the basic of PHP! You now know how to connect a PHP file to an html file, how to actually run the site with a a working php file, and what you can do with a PHP file, including how it can interact with user input. Thank you for reading, and have fun using PHP!

--

--