PHP for Non-Coders

As you might recall from our previous posts in this series, PHP:
– is in the category of Server Software
– is primarily used to create Web Apps
– is the software that powers WordPress, the leading blogging software
Let’s go a little deeper to gain a little better understanding of PHP for Non Coders.

PHP code is often mixed with HTML code and is usually processed by PHP Server Software which executes the code. After the PHP code is executed, the PHP server sends the resulting output to the requesting Web Browser.

In the programming world, it has been a tradition that the first introductory program displays the words “Hello World” on the screen.
In PHP you would create a file called something like Hello.php containing the following code:

<? echo “Hello world!”; ?>

If you accessed the Hello.php file in your browser the following would be displayed
“Hello World” would appear on the page.

The <html> and </html> are HTML tags that are used at the beginning and end of every HTML page.

The <? … ?> brackets in the code instructs the PHP Software to process the instructions within the brackets.
The echo command tells the PHP software to include the contents within the quotes, in our case “Hello World”, in the HTML page sent to the requesting browser.

Now let’s go a drop farther. This program:

<? for ($i = 1; $i < 4; $i++) { echo "Hello World being printed $i time(s) <br>"; } ?> </html>

Will display the following web page:

Hello World being printed 1 time(s)
Hello World being printed 2 time(s)
Hello World being printed 3 time(s)

We introduced the The <? … ?> brackets above which tells the PHP Software to interpret the PHP code within the brackets.

In this example we have introduced two new programming concepts
– the $i variable
– the for command

In our program, $i is called a variable because it will contain varying data values at different points in the execution of the program.

The for ($i = 1; $i < 4; $i++) command is called a loop, and it tells the PHP software to loop a certain number of times and execute the series of commands that follow.

In our example, a single command { echo “Hello World being printed $i time(s) <br>”; } will be executed a certain number of times.

Specifically our for command instructs the PHP software

$i = 1; create a variable name $1 and give it an initial value of 1
$i < 4; execute the commands that follow as long as the value of $i is less than 4
$i++ after you execute the commands, increment (add 1) to the value of $i

So the first time the command { echo “Hello World being printed $i time(s) <br>”; }
is executed, it would have the value of 1. The second time it would have the value of 2. And the third time it would have the value of 3.
After the echo command is executed with $i having the value of 3 it would be incremented to 4 and the $i < 4; would instruct the PHP software to stop processing the loop.

After an exercise like this, most programmers take a coffee break. So go ahead and when you come back you can take a look at one of the previous articles in our Coding for Non-Coders Series:

What is Coding Anyway? Coding for Non Coders – Introduction to the goals goals for this series.
Coding and Creating Software for Non-Coders – What software does and the five steps of creating software.
Understanding The Hardware and Software Jungle for Non-Coders – The categories of hardware and software.
Programming Languages for Non-Coders – Enumeration of commonly use programming and non-programming languages.
The Long, Short Journey of Opening a Web Page – The hardware, software and programming languages involved when you open up a web page.
A Quick Explanation of HTML for Non-Coders – A description of what HTML is and what it does.
A Quick Look at CSS for Non-Coders – A description of CSS and what it does.

Posted in Articles, Coding