You are viewing our Forum Archives. To view or take place in current topics click here.
~The Basics of PHP coding~
Posted:

~The Basics of PHP coding~Posted:

Decyfer
  • Resident Elite
Status: Offline
Joined: Apr 04, 201113Year Member
Posts: 203
Reputation Power: 10
Status: Offline
Joined: Apr 04, 201113Year Member
Posts: 203
Reputation Power: 10
Basic PHP

PHP is a server side scripting language used on the Internet to create dynamic web pages. It is often coupled with MySQL, a relational database server that can store the information and variables the PHP files may use. Together they can create everything from the simplest web site to a full blown business web site, an interactive web forum, or even an online role playing game.

Before we can do the big fancy stuff we must first learn the basics from which we build on.

1. Start by creating a blank file using any program that can save in plain text format.

2. Save your file as a .PHP file, for example mypage.php. Saving a page with the php. extension tells your server that it will need to execute the PHP code.

3. Enter the statement <?php to let the server know that there is PHP code coming up.

4. After this we would enter the body of our PHP program.

5. Enter the statement ?> to let the browser know the PHP code is done.


Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them. Here is an example:

<?php //on

//and

//off ?>

Everything between the <?php and ?> is read as PHP code. The <?php statement can also be phrased as simply <? if desired. Anything outside of these PHP tags is read as HTML, so you can easily switch between PHP and HTML as needed. This will come in handy later in our lessons.

Comments

If you want something to be ignored (a comment for example) you can put // before it as I did in our example on the previous page. There are a few other ways of creating comments within PHP, which I will demonstrate below:

<?php

//A comment on a single line

#Another single line comment

/* Using this method you can create a larger block of text and it will all be commented out */

?>

One reason you may want to put a comment in your code is to make a note to yourself about what the code is doing for reference when you edit it later. You may also want to put comments in your code if you plan on sharing it with others and want them to understand what it does, or to include your name and terms of use within the script.

PRINT and ECHO Statements


First we are going to learn about the echo statement, the most basic statement in PHP. What this does is output whatever you tell it to echo. For example:

<?php echo "I like About" ?>

This would return the statement I like About. Notice when we echo a statement, it is contained within quotation marks [].

Another way to do this is to use the print function. An example of that would be:

<?php print "I like About" ?>

There is a lot of debate about which is better to use or if there is any difference at all. Apparently in very large programs that are simply outputting text the ECHO statement will run slightly faster, but for the purposes of a beginner they are interchangeable.

Another thing to keep in mind is that all of your print/echoing is contained between quotation marks. If you want to use a quotation mark inside of the code, you must use a backslash:

<?php print "Billy said \"I like About too\"" ?>

When you are using more than one line of code inside your php tags, you must separate each line with a semicolon [;]. Below is an example of printing multiple lines of PHP, right inside your HTML:

<html>
<head>
<title>PHP Test Page</title>
</head>
<body>
<center><b>
<?php
print "I like About";
print "<br>";
print "Billy said \"I like About too\""
?>
</b></center>
</body>
</html>

As you can see, you can insert HTML right into your php print line. You can format the HTML in the rest of the document as you please, but remember to save it as a .php file.

Variables

The next basic thing you need to learn how to do is to set a variable. A variable is something that represents another value.

<?php
$like = "I like About";
print $like;
$num = 12345;
print $num;
?>

This sets our variable, $like, to our previous I like About statement. Notice again the quotation marks [] used, as well as the semicolon [;] to show the end of the statement. The second variable $num is an integer, and therefore does not use the quotation marks. The next line prints out the variable $like and $num respectively. You can print more than one variable on a line using a period [.], for example:

<?php
$like = "I like About";
$num = 12345;
print $like . $num;
print "<p>";
print $like . " " . $num;
print "<p>";
print "My favorite number is $num";
?>

This shows two examples of printing more than one thing. The first print line prints the $like and $num variables, with the period [.] to separate them. The third print line prints the $like variable, a blank space, and the $num variable, all separated with periods. The fifth line also demonstrates how a variable can be used within the quotation marks [""].

A few things to remember when working with variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.) Also note that if needed you can dynamically build variables.

The next basic thing you need to learn how to do is to set a variable. A variable is something that represents another value.

<?php
$like = "I like About";
print $like;
$num = 12345;
print $num;
?>

This sets our variable, $like, to our previous I like About statement. Notice again the quotation marks [] used, as well as the semicolon [;] to show the end of the statement. The second variable $num is an integer, and therefore does not use the quotation marks. The next line prints out the variable $like and $num respectively. You can print more than one variable on a line using a period [.], for example:

<?php
$like = "I like About";
$num = 12345;
print $like . $num;
print "<p>";
print $like . " " . $num;
print "<p>";
print "My favorite number is $num";
?>

This shows two examples of printing more than one thing. The first print line prints the $like and $num variables, with the period [.] to separate them. The third print line prints the $like variable, a blank space, and the $num variable, all separated with periods. The fifth line also demonstrates how a variable can be used within the quotation marks [""].

A few things to remember when working with variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.) Also note that if needed you can dynamically build variables.

Arrays

While a variable can hold a single piece of data, an array can hold a string of related data. Its use may not be apparent right away, but will become clearer as we start using loops and MySQL. Below is an example:

<?php
$friend[0] = "Justin";
$friend[1] = "Lloyd";
$friend[2] = "Alexa";
$friend[3] = "Devron";

$age["Justin"] = 45; $age["Lloyd"] = 32; $age["Alexa"] = 26; $age["Devron"] = 15;

print "My friends names are " . $friend[0] . ", " . $friend[1] . ", " . $friend[2] . ", and " . $friend[3];

print "<p>";

print "Alexa is " . $age["Alexa"] . " years old"; ?>

The first array ($friend) is arranged using integers as the key (the key is the information between the [brackets]) which is handy when using loops. The second array ($age) shows that you can also use a string (text) as the key. As demonstrated the values are called by print in the same way a regular variable would be.

The same principals apply to arrays as variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.)

Operands


You have probably all heard the term expression used in mathematics. We use expressions in PHP to preform operations and give an answer to a single value. These expressions are made up of two parts, the operators and the operands. The operands can be variables, numbers, strings, boolean values, or other expressions. Here is an example:

a = 3 + 4

In this expression the operands are a, 3 and 4

b = (3 + 4) / 2

In this expression the expression (3+4) is used as an operand along with b and 2.

Operators

Now that you understand what an operand is we can go into more detail about what operators are. Operators tell us what to do with operands, and they fall into three major categories:

Mathematical:
+(plus), - (minus), / (divided by), and * (multiplied by)

Comparison:
> (greater than), < (less than), == (equal to), and != (not equal to)

Boolean:
&& (true if both operands are true), || (true if at least one operand is true), xor (true if ONLY one operand is true), and ! (true if a single operand is false)

Mathematical operators are exactly what they are called, they apply mathematical functions to the operands. Comparison is also pretty straight forward, they compare one operand to another operand. Boolean however may need a little more explaining.

Boolean is an extremely simple form of logic. In Boolean every statement is either True or False. Think of a light switch, it must either be turned on or off, there is no in between. Let me give you an example:

$a = true;
$b = true;
$c = false;

$a && $b;
This is asking for $a and $b to both be true, since they are both true, this expression is TRUE

$a || $b;
This is asking for $a or $b to be true. Again this is a TRUE expression

$a xor $b;
This is asking for $a or $b, but not both, to be true. Since they are both true, this expression is FALSE

! $a;
This is asking for $a to be false. Since $a is true, this expression is FALSE

! $c;
This is asking for $c to be false. Since that is the case, this expression is TRUE

Conditional Statements

Conditionals allow your program to make choices. Following the same sort of boolean logic you just learned about, the computer can only make two choices; true or false. In the case of PHP this is accomplished using IF : ELSE statements. Below is an example of an IF statement that would apply a senior's discount. If $over65 is false, everything within the {brackets} is simply ignored.

<?php
$over65 = true;
$price = 1.00;
if ( $over65 )
{
$price = .90;
}
print "Your price is $" . $price;
?>

However, sometimes just the IF statement isn't enough, you need the ELSE statement as well. When using just the IF statement the code within the brackets either will (true) or will not (false) be executed before carrying on with the rest of the program. When we add in the ELSE statement, if the statement is true it will execute the first set of code and if it is false it will execute the second (ELSE) set of code. Here is an example:

<?php
$over65=true;
$price = 3.00;
if ($over65)
{
$discount =.90;
print "You have received our senior's discount, your price is $" . $price*$discount;
}
else
{
print "Sorry you do not qualify for a discount, your price is $" . $price;
}
?>

Nested Conditionals

One useful thing to remember about conditional statements is that they can be nested within each other. Below is an example of how the discount program from our example could be written to use nested IF:ELSE statements. There are other ways of doing this - such as using elseif () or switch () but this demonstrates how statements can be nested.

<?php
$age = 30;
$price = 3.00;
if ($age >65)
{
$discount =.90;
print "You have received our senior's discount, your price is $" . $price*$discount;
}
else
{
if ($age <18)
{
$discount =.95;
print "You have received our student's discount, your price is $" . $price*$discount;
}
else
{
print "Sorry you do not qualify for a discount, your price is $" . $price;
}
}
?>

This program will first check if they are eligible for the senior's discount. If they are not, it will then check if they are eligible for a student discount, before returning the non-discounted price.

Last but not least the credits,
Credits:
About-Alot of the info
Me-Info on it

Best Regards,

Decyfer

The following 2 users thanked Decyfer for this useful post:

-TaylorGang- (04-06-2011), BritChick (04-06-2011)
#2. Posted:
-TaylorGang-
  • Resident Elite
Status: Offline
Joined: Mar 06, 201113Year Member
Posts: 222
Reputation Power: 23
Status: Offline
Joined: Mar 06, 201113Year Member
Posts: 222
Reputation Power: 23
Thank you sir , Keep up the good work
#3. Posted:
SnowysModz
  • TTG Addict
Status: Offline
Joined: Mar 29, 201014Year Member
Posts: 2,228
Reputation Power: 116
Status: Offline
Joined: Mar 29, 201014Year Member
Posts: 2,228
Reputation Power: 116
Looks nice ...Keep up the good work
#4. Posted:
TTG_iRaaTeD
  • V5 Launch
Status: Offline
Joined: Aug 06, 201014Year Member
Posts: 3,718
Reputation Power: 187
Status: Offline
Joined: Aug 06, 201014Year Member
Posts: 3,718
Reputation Power: 187
Nice post man, keep up the good work.
#5. Posted:
Canning87
  • TTG Commander
Status: Offline
Joined: May 17, 201014Year Member
Posts: 6,839
Reputation Power: 311
Status: Offline
Joined: May 17, 201014Year Member
Posts: 6,839
Reputation Power: 311
Nice tut.

I might use this, i have never tried to learn php, the syntax looks hella confusing :O


Thanked.
#6. Posted:
irToad
  • TTG Contender
Status: Offline
Joined: May 17, 201014Year Member
Posts: 3,670
Reputation Power: 173
Status: Offline
Joined: May 17, 201014Year Member
Posts: 3,670
Reputation Power: 173
nice post man
#7. Posted:
Teh_Smithy
  • TTG Addict
Status: Offline
Joined: May 17, 201014Year Member
Posts: 2,241
Reputation Power: 108
Status: Offline
Joined: May 17, 201014Year Member
Posts: 2,241
Reputation Power: 108
Nice post, If it wasnt a copy and paste, which Im sure it wasnt right?
#8. Posted:
NyanDog
  • TTG Senior
Status: Offline
Joined: Jan 02, 201113Year Member
Posts: 1,252
Reputation Power: 67
Status: Offline
Joined: Jan 02, 201113Year Member
Posts: 1,252
Reputation Power: 67
<?php

echo 'I haven't coded PHP in a while';

?>
#9. Posted:
Forest
  • Rated Awesome
Status: Offline
Joined: Jul 13, 200915Year Member
Posts: 7,815
Reputation Power: 3052
Status: Offline
Joined: Jul 13, 200915Year Member
Posts: 7,815
Reputation Power: 3052
At least you gave credit to where all the text was located. And I do mean all the text

[ Register or Signin to view external links. ]
#10. Posted:
Decyfer
  • Resident Elite
Status: Offline
Joined: Apr 04, 201113Year Member
Posts: 203
Reputation Power: 10
Status: Offline
Joined: Apr 04, 201113Year Member
Posts: 203
Reputation Power: 10
TTG_GaLoway wrote At least you gave credit to where all the text was located. And I do mean all the text

[ Register or Signin to view external links. ]
Cool story you mad bro . ? I thin u just mad you never had a chance , thats right haters gonna hate
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.