PHP page 2

We'll tackle

1. integrating php with html and 2. making decisions

When writing PHP you'll end up writing a whole lot of HTML.
Trouble is if you want pure PHP, you'll always have to couch all your HTML code within "". And that's a pain.

So for the most part, you'll probably (unless using variables) mix your PHP and HTML together.
Starting with <? and then ending with ?> allows you to drop portions of PHP code into any html page. And the browser does all the hard work.

The real benefit of PHP is however to be able to cut the amount of code (html) that you have to write for any given scenario. It allows you to do anythning any other computer language can do eg. connect to databases, or count, or store things in variables.

1. INCLUDE

Creating PHP code in a separate file - let's call it "addit.php"
and adding the following to your code will save you loads of typing.
<? include "addit.php"; ?>
TIP: what I do to keep myself better organised is create a folder called "includes" - so you know the path to your include files will be
<? include "includes/addit.php"; ?>
and all your included files are in the same place.

2. Decisions

So you get to a fork in the road. You have to make a decision based on something. The simplest way to do this is

USING
if.. then..

Here's how it works.
if (this is true){We'll do this}
OR
if (this is untrue){We'll do this}
if ($diffdel=="on") {$v="1";} this is a real world example I have on one of my pages.
I am using the variable $diffdel (which was short for 'different delivery address')to give another variable a value, which I would use later.
From memory, if $v was equal to '0', the normal page was displayed, if ='1', it meant a different page was diaplayed. Not really important, just that a decision was made 'if something was true', then we'll run through some code.
TIP V important: The first set of round brackets are the capitals on the 9 and 0 on the keyboard - the second set (the curly brackets) are the capital versions of [ and ] on your keyboard.

OR
there are a few syptes of 'decision' forks such as switch, while, do, which have various uses. But you can look into these later. For now use 'if(this is true){do this}.

previous page     next page