Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

pixel-efx Coded FX PhotoShop Aid Xtreme Pixel 

Change Language

Who's online

There are currently 0 users and 11 guests online.

Lesson 10 - Review

I would like to use this lesson to go back over what we have learned about variables, strings, and control structures. If you feel you have mastered these then you are welcome to skip this lesson.

So once again, a variable can be given any of the following values, any of the following ways:

<?php
//Numbers:
$var1 "993"//$var1 is equal to 993
$var2 993;    //$var2 is equal to 993
$var3 '993'//$var3 is equal to 993

//Letters:
$var4 "G";
$var5 'G';

//Characters:
$var6 '%';
$var7 "+";

//Variables:
$var8 '8';

$var9 $var8;                 
//Variable 9 ($var9) is now equal to "8"

$var9++;
$var10 $var9;               
//Variable 10 is now equal to "9" ("8" plus one, or "++") 

$var11 $var10 $var8;       
//Variable 11 is now equal to 17

$var12 = ((0.1+0.7) * 10); // $var12 is equal to 8
$var13 '((0.1+0.7) * 10)'// $var12 is equal to "((0. 1+0.7)*10)"!
//Remember the '' means ABSOLUTE VALUE, while "" means PHP evaluates it. 

?>

A variable can hold a letter or any number.

Strings hold long "strings" of letters or numbers - like as a sentence. All of the following are strings:

Sentences:

<?php
$string1 
"Welcome to Learnphpfree.com";
$string2 "5448 Longroad, Somewhere, CA 99999";
$string3 "I love programming!?";

$string4 "Learn";
$string5 " PHP";
$string6 " Free!";
$string7 "$string4 $string5 $string6";         
//String 7 is "Learn PHP Free!"

$string8 "$var8 visitors to Learnphpfree.com";
//String 8 is equal to "8 visitors to Learnphpfree.com"

$string9 $var11" visitors to Learnphpfree.com"' Since yesterday';
//String 9 is equal to "17 visitors to Learnphpfree.com Since yesterday"
?>

You will be doing a lot with strings so make sure and practice with them on your own so that you know them well.

Control Structures are the logic of the code. Remember that if the "if" is FALSE than PHP while run the "else" statement, but if the "if" is true PHP will ignore the "else" statement.

Remember that these characters dictate the control structure:

<br />
">" means "Greater than"<br />
"+" means "Greater than or equal to"<br />

Here are a couple simple ones:

<?php
if (0) {
echo 
"1 is greater than 0";
} else {
echo 
"1 is not greater than 0";
}
//the "if" is TRUE because 1 is greater than 0



if (== 0) {
echo 
"1 is equal to 0";
} else {
echo 
"1 is not equal to 0";
}
//the "if" is FALSE because 1 is NOT equal to 0



if (!= 0) {
echo 
"1 is not equal to 0";
} else {
echo 
"1 is equal to 0";
}
//the "if" is TRUE because 1 is NOT equal to 0



if (>= 0) {
echo 
"1 is greater than or equal to 0";
} else {
echo 
"1 is not greater than or equal to 0";
}
//the "if" is TRUE because 1 is greater or equal to 0
?>

ADVANCED LOOPS:

For "&&" both have to be TRUE for the "if" to be true.
For "||" only one has to be TRUE for the "if" to be true.

<?php
if ((0) && (0)) {
echo 
"1 is greater than 0 AND 2 is greater than 0";
} else {
echo 
"1 is not greater than 0 AND/OR 2 is not greater than 0";
}
//the "if" is TRUE because BOTH are greater than 0



if ((0) && (0)) {
echo 
"1 is greater than 0 AND 2 is greater than 0";
} else {
echo 
"1 is not greater than 0 AND/OR 2 is not greater than 0";
}
//the "if" is TRUE because BOTH are greater than 0



if ((0) || (== 0)) {
echo 
"1 is greater than 0 OR 2 is equal to 0";
} else {
echo 
"1 is not greater than 0 OR 2 is not equal to 0";
}
//the "if" is TRUE because the first statement is TRUE even though the second is FALSE.



if ((0) || (>= 0)) {
echo 
"1 is less than 0 OR 2 is greater than or equal to 0";
} else {
echo 
"1 is not less than 0 OR 2 is not greater than or equal to 0";
}
//the "if" is TRUE because the first statement is FALSE but the second is TRUE.

?>

Last I would like YOU to try to make your own "if" statements and try making and messing with strings and variables too! Here is a zip archive of the loops, variables, and strings files. Upload them to your own site and experiment changing them. You need to make sure you understand them well.


Submitted by David on November 9, 2006 - 6:17pm.
printer friendly version

Cheers again another great tutorial

Even though this one was revision it still made variabls and strings more clearly for me. I didn't know that the OR/|| or the AND/&& was used in PHP at all.
Thanks for the last 9 tutorials and your movies do help alot.


I have Confusion

Rana Zeeshan
Hello Every body
i Have confusion in this lesson
I want ask a question HOW is $var11 is equal to 17?

?php
$string1 = "Welcome to Learnphpfree.com";
$string2 = "5448 Longroad, Somewhere, CA 99999";
$string3 = "I love programming!?";

$string4 = "Learn";
$string5 = " PHP";
$string6 = " Free!";
$string7 = "$string4 $string5 $string6";
//String 7 is "Learn PHP Free!"

$string8 = "$var8 visitors to Learnphpfree.com";
//String 8 is equal to "8 visitors to Learnphpfree.com"

$string9 = $var11. " visitors to Learnphpfree.com". ' Since yesterday';
//String 9 is equal to "17 visitors to Learnphpfree.com Since yesterday"
?>


Separated Variables

Sorry, I separated the variables in this lesson into different parts to make it easier to follow. If you look at the code block right above that you will see we set $var11 = $var10 + $var8 (Which equals 17).


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati