Lesson 6

Working with WAMP & MAMP

If you’re on Windows, start your WAMP and select www directory. 

www Dir

Your computer should show you the www folder in which there is a bunch of files and folders. On Windows www folder is located in C:\wamp64. From now on we will be creating and keeping all our web pages and websites in www folder. 

If you’re on Mac OsX and using MAMP instead of www you will have a htdocs folder located in Macintosh HD\Applications\MAMP

Now that we know where our local websites reside, let’s see how they look in our browser. In WAMP open localhost and on MAMP press start servers.

   

Your default browser should now load a starting configuration page. Look at the address bar – it should start with “loacalhost” – this means that the page your viewing in browser has been delivered to you via your local server.

So far we have always been calling our main page in each of the websites folder “index.html”This is not only a convention. When you try to access a website directory (folder) in the browser, the server always looks for the index file. If it finds it – it serves it automatically. Look at the WAMP server configuration page on the illustration above. What you see is the content of the index.php file.

Now let’s open our www folder on Windows machine and rename that inndex.php file to be wamp-index.php.

If you re-fresh the browser window you should see something like the we have on screen below:

This is our index of the www directory. The default index.php or index.html where not there, so the server generated a page with a list of files and folders for us.

PHP

PHP is a server scripting language. It means that it is being interpreted by server before we get to see the outcome / results of a given script in a browser. You will understand better what that means when we get to examples, but before that – let’s cover the basis by digging right into it.

In your www folder create a new folder called “testing-php”. Inside that folder create a new file – index.php.  Yes – if we want our server to understand that it is a php file, we need to give it a “.php” extension – not “.html”.

Let’s now open this index.php file in your text editor and start writing some PHP.

If you use Atom as a text editor, open the file and start typing  “htm” and as hit enter as soon as you see the drop down list suggesting “html”.

This will produce a html starting template for us with <head> and <body> tags produced automatically, so we don’t have to type it by hand.

Within the body tag let’s now add a PHP script.

Even though our file has extension “.php” we can write plain HTML and CSS within it. So for the server to know exactly that what we write now is in fact PHP – we need to open and close PHP block. We open with <?php and close with ?>

The echo command tells the server to write to the browser what ever you put in the quotation marks(). It is extremely important to end every line of PHP with the semicolon (;).

Using variables

One of the great advantages of scripting languages are the variables. This might sound familiar from your math classes where you also use variables, like x,y or z. In PHP we declare variables with the $ symbol in front of your variable name. So PHP variable could look like $x or $z. The rule is that you musn’t start variable names with numbers, don’t use special characters (#,%,*,-,+,=,^,@), non-regular letters (ć,ś,â,ç,ê etc.) or spaces. By convention you should start variable name with lower case letter. You are allowed to mix cases (camel case) or use underscores – but decide which convention you prefer and stick to it. It is also very important that your variable names are meaningful to you. These are sample valid variable names: $student, $firstName, $last_name, $address1, $address_2

When we set value to a variable, we say we declare a variable.

Let’s declare some variables now:

Now let’s use echo command to write $first_name in the browser.

$first_name and $last_name are variables of type string. This means they contain text (i.e. “John”). The content of string variable we always need to declare with quotation marks (double “” or single ”). We don’t need quotation marks when we declare numbers:

String concatenation

When you want to join two or more strings you do string concatenation. You do it with the dot (.) symbol.

The above script will concatenate 2 strings – $first_name and $last_name and will output: JohnSmith. Let’s add a 3rd string to have a space character between these two words:

Great – now we get: John Smith.

Echo Markup

Echo can output not only plain text – you can also use it to print HTML:

or

Sometimes it’s better to concatenate strings when declaring variables like this:

Mixing echo and HTML

On your page you could do:

but you could as well do

For writing something like the above, you might prefer to use a shorthand for printing variables. Instead of full opening and closing <?php  ?> and writing echo statement, you can do:

PHP include

Very powerful feature of scripting languages is the ability to use includes. It means that you can put a portion of your HTML into one file and include it another file.

For example you could create a file called head.php and put all your head markup there:

Do the same for the footer code and then include them in your index.php with include command followed by a path to the file:

This way you don’t need to repeat yourself typing head and footer markup on all of your pages (i.e. about, contact etc.). You just include the head.php and foot.php

 

« Lesson 5 Index Lesson 7 »