Forum rules
Please read our Guide to Making Ozzu Tutorials if you would like to submit your own tutorials.
TUTORIAL: Learning PHP (Part 4)
Learning PHP - Part 4
This tutorial assumes that you know everything talked about in Part 1, Part 2, and in Part 3.
This part of the series is basically relearning everything and revisiting a few things that were talked about in the previous sections. Some programming techniques and troubleshooting would be given, and some foresight to the following parts in the series would be provided to think about.
(Also, some new things would be taught, so don't skip thinking that you know everything and don't need to relearn anything).
We have talked about if... elseif... else statements, so now we must expand on their uses and functionality. Let me show you a basic 'outline' of the if conditional statement that is shown many times throughout the web on PHP tutorials.
That basically sums up the conditional if statement.
It's also time for me to provide you with a simpler if... (elseif...) else statement for a simple function.
This form of if... elseif... else... statement is mainly used to define variables that depends on a certain variable. This is basically a one liner... but before I go ahead and explain it to you, let me show you the actual code example:
See the syntax for the one-liner if... elseif... else statement? Let me explain it for you...
Not much of explaining, but you put the condition the condition you need to check for in the 'IF' section... if that holds to be true then you need to put the result in the 'then' statement, or otherwise, put the default to 'else' part of the statement.
There is not much more to talk about here about if... elseif... else statements other then to give you the term of what it actually is... a conditional statement.
A conditional statement merely means that a certain part would be executed that meets to the written demand (or condition in this case). You've seeing that in action with the if... else... statement and so you know what it means... if $a == $b then do $c otherwise do $d.
There are other conditional statements available for the programmer rather then just if.. elseif... else statements. There is also the 'switch' statement. It basically flips the switch accordingly to what the condition meets (to the demand) and executes that part of the script. Let's see an example here:
And the famouse 'outline' for it is:
See all that? What it does is mainly check if $condition (the expression) is equal to any of the cases available... you don't have to have three or two or whatever... you can have as many cases as you need to get the job done right.
The 'default' part of the script is equivalent to 'else' in the if... elseif... else statement.
The break; at the end of the case is required to tell the script that the 'case' has ended... otherwise it would execute the other cases until it reaches the end of the switch conditional, or until it reaches a break... whichever comes first.
You can test that out by executing the following script and putting a break; at various different cases, or maybe only after the default... or none at all...
What it is doing is incrementing the $i every time the case is met... so if you see a '4' then all 4 cases have being went through... if you see '3' then only the first 3 cases were being executed... If you see a '55' that the default was executed as well... otherwise, the 'default' wasn't executed... (See the use of a conditional statement used in human terms? ['otherwise' being equivalent to 'else']).
A break; is not only for the switch operator, it could be used to break out of loops and that fun stuff.
Alright... no pun intended, but let's revisit the strings... while we are here, let's talk about other data-types we might encounter and define them and describe their uses.
Remember when I said that strings are text that have a 'quote' around it? (Yes, like that quote
). Well, that is mainly true... but let's consider the following case.
We know that $str is a string... but what about $var? Is it a string? Well, it doesn't have the quotes around it, so it must not be a string. WRONG! It is a string.
How come? $str has being set as a string... it would always keep that data-type even if it was set to a different variable as a variable like it has being to $var in the example above.
Consider this in a real life situation (Although, I'm sure you understand, but let me pretend to be smart
), you are writing a letter. You are putting words on a piece of paper just as a quoted string is being set to a variable.
The next thing you do is give that letter to your friendly neighbor 'Bob' to tell him that you can't visit him on Saturday's anymore... it is still a letter when he takes it... right? So, when the $str was set to be the same as $var it is still a string (Not those dangly thin stuff that some people abuse... but a PHP string of text).
Right, I said I would talk about other data-types, and so I will. Let me define you with some data-types that are available within PHP. (NOTE: There are eight (8) available in PHP).
An integer number is set to a variable as a simple number... no quotes around it or anything. (e.g: $var = 1;). It could be any number that doesn't have a decimal in it.
A floating point number is set to a variable as a simple decimal... no quotes around it or anything. (e.g: $var = 1.2345;). It could be any number with a decimal in it.
NOTE: And integer or a floating point numbers could be positive or negative (-45;-4.5 -or- +45;+4.5), but I don't recommend putting that plus (+) when the number is positive, it is positive by default when you simply write (45; 4.5)... you might confuse the script into thinking that you are adding if you don't do it right.
A string is a string of text wrapped in double or single quotes (e.g: $var = 'string'; $var2 = "string2";).
Booleans are anything that equal FALSE or 1 or true... The following values would equal FALSE.
[quote=http://webcheatsheet.com]
[/quote]
And like the quote said... any other value that are not on the list would be a TRUE, be it string, array or whatever.
We have already covered arrays and will return to it later on in this tutorial, so there is nothing much for me to say about them here.
Objects are associated well with classes and we would work for them on a later tutorial in this series, but for now, just know that Objects are similar to arrays, except with more functionality within them.
Resources will be covered later on, but know that they are more associated to SQL commands.
And last but not least, the null datatype. They are basically say nothing... just a place holder for a variable... some programmers use it to release variables from lengthy definitions to a simple null... freeing CPU and memory usage, making the script go faster.
The following example is what I'm talking about. You don't have to read it because it's junk... just showing you some size.
But I would recommend not to worry about Garbage and Garbage Dumping (We will talk about this in a later tutorial), as PHP takes care of it pretty fairly by default.
We've already talked about how to set and echo variables. It may look like there is not much to do with them but set them and echo or return them, or possible use them in the script validation. While that may be true, there are different ways to set them and each way is different from the other. Let us now explore those ways.
There is the conventional way to set the variable. We've already talked about this one... an example to refresh your mind is:
Just for the record, variable names could be seperated by a line (e.g: $variable_name;);
You can also set a variable by "reference". There are numerous things you can do with this. An example of this is:
What that does is setting the $var to the source of $another_var rather than it's value.
Let me describe the differences between the two ways here:
The conventional way of setting a variable sets the variable to the value of a variable ($var = $var2;) while the Referential Setting of a variable sets the variable to the source's value of another variable. What I mean is that $var and $another_var point to the same content.
This means that $var and $another_var are not pointing at each other... they are not set by the value of each other, but the value of the source. So far, this is what I will tell you about Referential Setting of a variable... in a later tutorial I will go more in depth with this when we start talking about functions and classes.
Well, as always and following the same format as any other tutorial in this series, you have learned some good things about PHP and some uses of it
(Hopefully).
The following terms used in the tutorial were made up by me
<- Go to part 3 || Part 5 is in creation
If you have any questions or suggestions, please PM me
Yeah!
I used some sources for this.
Conditional Statements and some data-types
Data-Types
Introduction
This tutorial assumes that you know everything talked about in Part 1, Part 2, and in Part 3.
This part of the series is basically relearning everything and revisiting a few things that were talked about in the previous sections. Some programming techniques and troubleshooting would be given, and some foresight to the following parts in the series would be provided to think about.
(Also, some new things would be taught, so don't skip thinking that you know everything and don't need to relearn anything).
Conditional statements - Revisited
We have talked about if... elseif... else statements, so now we must expand on their uses and functionality. Let me show you a basic 'outline' of the if conditional statement that is shown many times throughout the web on PHP tutorials.
if (condition_1) {
statement_1
}
[elseif (condition_2) {
statement_2
}]
...
[elseif (condition_n_1) {
statement_n_1
}]
[else {
statement_n
}]
statement_1
}
[elseif (condition_2) {
statement_2
}]
...
[elseif (condition_n_1) {
statement_n_1
}]
[else {
statement_n
}]
- if (condition_1) {
- statement_1
- }
- [elseif (condition_2) {
- statement_2
- }]
- ...
- [elseif (condition_n_1) {
- statement_n_1
- }]
- [else {
- statement_n
- }]
That basically sums up the conditional if statement.
It's also time for me to provide you with a simpler if... (elseif...) else statement for a simple function.
This form of if... elseif... else... statement is mainly used to define variables that depends on a certain variable. This is basically a one liner... but before I go ahead and explain it to you, let me show you the actual code example:
<?php
$b = 15 // The variable to check in our conditional block
// Setting the variable using the shortened if... elseif... else statement
$variable = (($b <= 14) ? 'It is less then or equal to fourteen' : 'It is not less then or equal to fourteen');
?>
$b = 15 // The variable to check in our conditional block
// Setting the variable using the shortened if... elseif... else statement
$variable = (($b <= 14) ? 'It is less then or equal to fourteen' : 'It is not less then or equal to fourteen');
?>
- <?php
- $b = 15 // The variable to check in our conditional block
- // Setting the variable using the shortened if... elseif... else statement
- $variable = (($b <= 14) ? 'It is less then or equal to fourteen' : 'It is not less then or equal to fourteen');
- ?>
See the syntax for the one-liner if... elseif... else statement? Let me explain it for you...
((IF) ? then : else);
Not much of explaining, but you put the condition the condition you need to check for in the 'IF' section... if that holds to be true then you need to put the result in the 'then' statement, or otherwise, put the default to 'else' part of the statement.
There is not much more to talk about here about if... elseif... else statements other then to give you the term of what it actually is... a conditional statement.
A conditional statement merely means that a certain part would be executed that meets to the written demand (or condition in this case). You've seeing that in action with the if... else... statement and so you know what it means... if $a == $b then do $c otherwise do $d.
There are other conditional statements available for the programmer rather then just if.. elseif... else statements. There is also the 'switch' statement. It basically flips the switch accordingly to what the condition meets (to the demand) and executes that part of the script. Let's see an example here:
<?php
$condition = 'lunatic';
switch($condition)
{
case 'manic':
// Do code for manic
break;
case 'lunatic':
// Do code for lunatic
break'
case 'president':
// Do code for president
break;
default:
// Do the 'else' part... the default of the script
break;
}
?>
$condition = 'lunatic';
switch($condition)
{
case 'manic':
// Do code for manic
break;
case 'lunatic':
// Do code for lunatic
break'
case 'president':
// Do code for president
break;
default:
// Do the 'else' part... the default of the script
break;
}
?>
- <?php
- $condition = 'lunatic';
- switch($condition)
- {
- case 'manic':
- // Do code for manic
- break;
- case 'lunatic':
- // Do code for lunatic
- break'
- case 'president':
- // Do code for president
- break;
- default:
- // Do the 'else' part... the default of the script
- break;
- }
- ?>
And the famouse 'outline' for it is:
switch (expression) {
case label_1:
statements_1
[break;]
case label_2:
statements_2
[break;]
...
default:
statements_n
[break;]
}
case label_1:
statements_1
[break;]
case label_2:
statements_2
[break;]
...
default:
statements_n
[break;]
}
- switch (expression) {
- case label_1:
- statements_1
- [break;]
- case label_2:
- statements_2
- [break;]
- ...
- default:
- statements_n
- [break;]
- }
See all that? What it does is mainly check if $condition (the expression) is equal to any of the cases available... you don't have to have three or two or whatever... you can have as many cases as you need to get the job done right.
The 'default' part of the script is equivalent to 'else' in the if... elseif... else statement.
The break; at the end of the case is required to tell the script that the 'case' has ended... otherwise it would execute the other cases until it reaches the end of the switch conditional, or until it reaches a break... whichever comes first.
You can test that out by executing the following script and putting a break; at various different cases, or maybe only after the default... or none at all...
<?php
$var = 1;
$i = 0;
switch($var)
{
case 1:
++$i;
case 2:
++$i;
case 3:
++$i;
case 4:
++$i;
default:
$vars = 55;
}
echo $i . "<br />\n";
if(isset($vars))
{
echo $vars;
}
?>
$var = 1;
$i = 0;
switch($var)
{
case 1:
++$i;
case 2:
++$i;
case 3:
++$i;
case 4:
++$i;
default:
$vars = 55;
}
echo $i . "<br />\n";
if(isset($vars))
{
echo $vars;
}
?>
- <?php
- $var = 1;
- $i = 0;
- switch($var)
- {
- case 1:
- ++$i;
- case 2:
- ++$i;
- case 3:
- ++$i;
- case 4:
- ++$i;
- default:
- $vars = 55;
- }
- echo $i . "<br />\n";
- if(isset($vars))
- {
- echo $vars;
- }
- ?>
What it is doing is incrementing the $i every time the case is met... so if you see a '4' then all 4 cases have being went through... if you see '3' then only the first 3 cases were being executed... If you see a '55' that the default was executed as well... otherwise, the 'default' wasn't executed... (See the use of a conditional statement used in human terms? ['otherwise' being equivalent to 'else']).
A break; is not only for the switch operator, it could be used to break out of loops and that fun stuff.
Back on the Strings with other data-types
Alright... no pun intended, but let's revisit the strings... while we are here, let's talk about other data-types we might encounter and define them and describe their uses.
Remember when I said that strings are text that have a 'quote' around it? (Yes, like that quote
<?php
$str = 'This is a string';
$var = $str;
?>
$str = 'This is a string';
$var = $str;
?>
- <?php
- $str = 'This is a string';
- $var = $str;
- ?>
We know that $str is a string... but what about $var? Is it a string? Well, it doesn't have the quotes around it, so it must not be a string. WRONG! It is a string.
How come? $str has being set as a string... it would always keep that data-type even if it was set to a different variable as a variable like it has being to $var in the example above.
Consider this in a real life situation (Although, I'm sure you understand, but let me pretend to be smart
The next thing you do is give that letter to your friendly neighbor 'Bob' to tell him that you can't visit him on Saturday's anymore... it is still a letter when he takes it... right? So, when the $str was set to be the same as $var it is still a string (Not those dangly thin stuff that some people abuse... but a PHP string of text).
Right, I said I would talk about other data-types, and so I will. Let me define you with some data-types that are available within PHP. (NOTE: There are eight (8) available in PHP).
- Integer numbers
- Floating point numbers
- Strings
- Booleans
- Arrays
- Objects
- Resouces
- Null
An integer number is set to a variable as a simple number... no quotes around it or anything. (e.g: $var = 1;). It could be any number that doesn't have a decimal in it.
A floating point number is set to a variable as a simple decimal... no quotes around it or anything. (e.g: $var = 1.2345;). It could be any number with a decimal in it.
NOTE: And integer or a floating point numbers could be positive or negative (-45;-4.5 -or- +45;+4.5), but I don't recommend putting that plus (+) when the number is positive, it is positive by default when you simply write (45; 4.5)... you might confuse the script into thinking that you are adding if you don't do it right.
A string is a string of text wrapped in double or single quotes (e.g: $var = 'string'; $var2 = "string2";).
Booleans are anything that equal FALSE or 1 or true... The following values would equal FALSE.
[quote=http://webcheatsheet.com]
- Boolean False
- Integer zero (0)
- Double zero (0.0)
- Empty string and string "0"
- Array with no elements
- Object with no elements
- Special value NULL
[/quote]
And like the quote said... any other value that are not on the list would be a TRUE, be it string, array or whatever.
We have already covered arrays and will return to it later on in this tutorial, so there is nothing much for me to say about them here.
Objects are associated well with classes and we would work for them on a later tutorial in this series, but for now, just know that Objects are similar to arrays, except with more functionality within them.
Resources will be covered later on, but know that they are more associated to SQL commands.
And last but not least, the null datatype. They are basically say nothing... just a place holder for a variable... some programmers use it to release variables from lengthy definitions to a simple null... freeing CPU and memory usage, making the script go faster.
The following example is what I'm talking about. You don't have to read it because it's junk... just showing you some size.
<?php
$lengthy_var = 'something here that would take up a lot of space and bytes,
that could possibly go over a few lines and cause something of a execution time lag,
even by a mere nanosecond that would be unnoticeable by the user, but it\'s there... you know
it is there because you are the programmer and the one who made a lengthy variable holding
a lengthy definition that takes up a lot of space. Heh, this wasn\'t fun to type... believe in me
and hope for yourself :). As you could clearly see, that this text was clearly jibberish and wasn\'t meant for you to read. But if you did read it, then I don\'t really mind it... just means you either have a lot of time, or you didn\'t believe me when I told you that this is junk. Good luck!';
// Do some stuff with $lengthy_var
$length_var = null;
?>
$lengthy_var = 'something here that would take up a lot of space and bytes,
that could possibly go over a few lines and cause something of a execution time lag,
even by a mere nanosecond that would be unnoticeable by the user, but it\'s there... you know
it is there because you are the programmer and the one who made a lengthy variable holding
a lengthy definition that takes up a lot of space. Heh, this wasn\'t fun to type... believe in me
and hope for yourself :). As you could clearly see, that this text was clearly jibberish and wasn\'t meant for you to read. But if you did read it, then I don\'t really mind it... just means you either have a lot of time, or you didn\'t believe me when I told you that this is junk. Good luck!';
// Do some stuff with $lengthy_var
$length_var = null;
?>
- <?php
- $lengthy_var = 'something here that would take up a lot of space and bytes,
- that could possibly go over a few lines and cause something of a execution time lag,
- even by a mere nanosecond that would be unnoticeable by the user, but it\'s there... you know
- it is there because you are the programmer and the one who made a lengthy variable holding
- a lengthy definition that takes up a lot of space. Heh, this wasn\'t fun to type... believe in me
- and hope for yourself :). As you could clearly see, that this text was clearly jibberish and wasn\'t meant for you to read. But if you did read it, then I don\'t really mind it... just means you either have a lot of time, or you didn\'t believe me when I told you that this is junk. Good luck!';
- // Do some stuff with $lengthy_var
- $length_var = null;
- ?>
But I would recommend not to worry about Garbage and Garbage Dumping (We will talk about this in a later tutorial), as PHP takes care of it pretty fairly by default.
Variables
We've already talked about how to set and echo variables. It may look like there is not much to do with them but set them and echo or return them, or possible use them in the script validation. While that may be true, there are different ways to set them and each way is different from the other. Let us now explore those ways.
There is the conventional way to set the variable. We've already talked about this one... an example to refresh your mind is:
$variable = 'The setting';
$variable = $anotherVariable;
$variable = $anotherVariable;
- $variable = 'The setting';
- $variable = $anotherVariable;
Just for the record, variable names could be seperated by a line (e.g: $variable_name;);
You can also set a variable by "reference". There are numerous things you can do with this. An example of this is:
$var =& $another_var;
What that does is setting the $var to the source of $another_var rather than it's value.
Let me describe the differences between the two ways here:
The conventional way of setting a variable sets the variable to the value of a variable ($var = $var2;) while the Referential Setting of a variable sets the variable to the source's value of another variable. What I mean is that $var and $another_var point to the same content.
This means that $var and $another_var are not pointing at each other... they are not set by the value of each other, but the value of the source. So far, this is what I will tell you about Referential Setting of a variable... in a later tutorial I will go more in depth with this when we start talking about functions and classes.
Conclusion
Well, as always and following the same format as any other tutorial in this series, you have learned some good things about PHP and some uses of it
The following terms used in the tutorial were made up by me
- Conventional Way
<- Go to part 3 || Part 5 is in creation
If you have any questions or suggestions, please PM me
Sources
Yeah!
Conditional Statements and some data-types
Data-Types
Learn PHP | I got 10 PHP tutorials! Check them out!
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
June 24th, 2009, 11:22 pm
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 1 post
- Moderator: Tutorial Writers
- Users browsing this forum: No registered users and 3 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum


