Learn PHP in 8 Weeks – Week 1

Posted on at


Learning PHP in 8 Weeks

Want to learn PHP? I have broken down learning PHP in 8 week program. By spending just a few times every week, before you learn it you’d better know all the basics of this language. This is a great way to learn a new language over the free hours of the days or weeks, or any time! Each lesson is short, and can be read and mastered very quickly. You then have the rest of the week to reread and practice the skill or you move on to the next lesson, you can learn two or three lesson in a week. Ok, ready? Let’s start!

Week 1: – Your first programYour first PHP program will be the traditional “Hello World”, the typical first program in any language you might learn. In this lesson you’ll learn how to start and end a PHP file, as well as two different ways you can great the world. You will also learn how to format your hello using HTML right inside of your PHP.

Getting started with PHP

So think you are totally new to PHP, and don’t know how to get started. We start by providing some materials required by starting PHP. To start out you’re going to need some sort of plain text editor. This can be a program you can download from internet, or it can be something like Notepad (PC) or TextEdit (Mac) that comes free with your computer operating systems. As most tutorials also recommend you need a basic knowledge of HTML, because the structure of any web apps are made by HTML, and also you have better to know CSS for Modifying and customizing your made app’s design.

So we first start with the most basic and traditional of programs, every language’s first shoot out, the “Hello World”.

Open any text editor program like notepad, Dreamweaver or any other, Start your page with an open PHP bracket. It looks like this:

<?php

After this tag we are writing the code that shows “Hello World” in browser. There are two ways you can do that, one is print and the other is echo.

Here is an example of how to use print:

print “Hello World!”;

And the example of how to use echo:

echo “Hello World!”;

As you see each (print and echo) starts with a PHP code, then we put our phrase or sentence inside the quotes (“Hello World”) to show for interface users. At the end we put a semicolon”;” to end the line.

We then have to end our code by closing the PHP tag that we opened at the beginning of the program. For that, write the below small closing tag:

?>

So the whole code should look like this:

<?php
echo “Hello World”;
?>

You can add comment for your information on the code by putting double backslash. Anything on the line with the double backslash will be ignored when the PHP runs and don’t show, it is only there as notes for the programmer. See the example:

<?php
// this could be print or echo
print “Hello World”;
?>

Now we write the above code with a variable. In PHP variables always start with a dollar sign. So, for example $name is a variable, but only name itself is not variable. We can then assign a value to the variable by putting equal sign between variable and our value. So, we will show this time our greeting phrase like this:

$greet = “Hello World”;

Now type our above codes again with the variable:

<?php
$greet = “Hello World”;
// this could be print or echo
echo $greet;
?>

Notice: no matter if we put the variable inside the quotations or no when using print or echo.

<?php
$greet = “Hello”;
// this could be print or echo
print “$greet World!”;
?>

If you remember I told you before starting this lesson, that you have better to know HTML. That’s because we format some time our texts inside a PHP. The HTML tag can be put outside of php or inside PHP codes. In this example we simply make our text bold.

Here is an example with the HTML outside of the PHP:

<b>
<?php
$greet = “Hello”;
// this could be print or echo
echo “$greet World!”;
?>
</b>

And here is an example with the HTML inside of the PHP:

<?php
$greet = “Hello”;
// this could be print or echo
print “<b>$greet World!</b>”;
?>

Notice: make sure if you put the HTML inside of the PHP, it should be inside the quotations of print and echo.

In this lesson we learned what is needed for writing a php code, how to start and combine html tags with PHP, how to work with variable and assign values to it and use comments inside php codes.

Now save the above code in Plain Text Format with the extension .php. Then you can show with your local php service in your computer or upload to your server.

Congratulations, you have just written your first PHP program!

You can read this article on my own website Prowebsters



About the author

jkahmadi

Freelance Web and Graphic Designer, Blogger and Social Media Activist.

Subscribe 0
160