Paxton
Banned User
Welcome in Paxton's Web Development School Part: 2
If you are new in PHP and you didn't read Part 1 I recommend you to do it
Today we are going to learn few things, of course we will practise all of it and at the end I will give you homework, I know you love it hehe
So, let's start.
Open your workspace (index.php in your tut folder)
Remove everything from there, or save to other file, if you have forgotten what we learnt of last lessons you better read it quickly again.
Put in your index.php starting and ending tag:
I didn't tell you that on last lesson, but you don't have to use
you might just use
but it is better to use the full one, because it might also be some other language like a
and some server don't recognize them.
Now we are going to learn loops.
Loops might be used if you want to repeat the code as many times as you want, for example you want to display numbers from 1 to 100 on your website.
Let's see the example code:
Okay, the function is called 'for'
We passed 3 arguments to the functions, first:
We are declaring that X equals to 1
Second is argument, that works exactly as 'if' the loop weill be repeated until this 'if' won't be TRUE.
The last argument is what happens every repeat, $x++ means that every repeat 1 will be added to $x
I know it might be hard to understand, but don't worry I didn't understand this function first time as well It's a bit complicated.
After running this code you should see on your website:
The secod argument is:
So the loop will be repeated until $x is 10.
There is also second loop, it more easier, it's called 'while'
Let's see an example.
This code will show exactly same thing as the 'for' example
In this function you just give one argument, it's exactly as 'if' the loop will be repeated until this argument is TRUE
So, first we declare $x = 1;
So $x equals to 1.
Then we make 'while' which will be repeated until $x equals to 10 or it's smaller.
And the functions inside loop is showing actuall $x value, and then add's 1 to it.
Return of this code should be:
Also, there is one more loop called 'Do... while' but I won't be telling you about it, because it's not so inportant and in my experience, I never used it.
But still you can read about it here:
PHP: do-while - Manual
Some times we need to stop repeating in loop and start it again, to do this we need to add function
Example:
In this example, the return will be the even numbers.
If you don't remember from last lesson % returns the rest of dividing, so if the rest of dividing by 2 is 0 then it must be even number
Also, sometimes you need to exit the loop, to do this you use function called:
Example:
Now, we are going to learn about 'Switch' it does the same job as 'if' but it's written differently.
Let's see an example:
Okey, let's explain this.
First we declare variable: $i which is equal to 3.
Now we make Switch and the only one argument we give is the name of variable.
The 'case' as the name says tells us what we do when $i is this case.
So, if the
$i is 0 then go to next case
$i is 1 then go to next case
$i is 2 then go to next case
$i is 3 then go to next case
$i is 4 then print text "Variable i is smaller or same as 3"
Then we declare function break; which finish this block of 'case'
Then we declare next case: 4
So if the $i is 4 text: "Variable i is equal to 4" will be printed.
We declare break; again and now function 'default' works same as 'else' in 'if' this block will be executed only if no case has been executed for example, if the $i is 5.
Now we are going to learn about tables, this is very usefull function if PHP, and it's very easy to handle data.
For example, you want to give info about your car to variables, until now you would do it like this:
etc.
or
But there is better way to do it.
One name of the variable might handle all this data.
Let's see an example.
So now, if you want to call it you use this for example:
It's much better and it's very usefull for example in writing configs, example:
Now, we are going to learn one of the most important things in PHP, and one of the most usable
FUNCTIONS! We are going to write our own functions.
First, we gonna try something easy.
Example:
We declare new function by function 'function'
then we give name of functions and brackets, in there we give arguments, but our functions is easy and we don't need any, then we write what our function will do, in this case will print text "My Text"
if you want to declare you own function now, just write:
Because this is your name of functions, and now everytime you will call this function text "My Text" will be shown.
Let's write function with arguments.
Now, as you can see when we declare new function we gave two arguments, so now when you declare this function in your code you need to pass them.
So now, if you call function you need to type in brackets your variables, in this case I just used numbers:
So, now function will add 1+4
But why did I use echo? Why I just don't used this function like that one before? Because in this function I didn't use echo but I used 'return' this is new function that you don't know yet, it returns the value but I doesn't display sometimes it's important on website, because you don't want to display the text but do something else with it.
Now, it's time for your homework.
1)
Make a loop for me that will show only ODD numbers from 1 to 100.
2)
Make for me function called calculator, where I will be able to give 3 arguments.
First number, Second number, type of calucalion
example
Or
Don't forget to leave me rep point
If you are new in PHP and you didn't read Part 1 I recommend you to do it
Today we are going to learn few things, of course we will practise all of it and at the end I will give you homework, I know you love it hehe
So, let's start.
Open your workspace (index.php in your tut folder)
Remove everything from there, or save to other file, if you have forgotten what we learnt of last lessons you better read it quickly again.
Put in your index.php starting and ending tag:
PHP:
<?php
?>
I didn't tell you that on last lesson, but you don't have to use
PHP:
<?php
you might just use
PHP:
<?
but it is better to use the full one, because it might also be some other language like a
Code:
<?xml
and some server don't recognize them.
Now we are going to learn loops.
Loops might be used if you want to repeat the code as many times as you want, for example you want to display numbers from 1 to 100 on your website.
Let's see the example code:
PHP:
<?php
for( $x = 1; $x <= 10; $x++ )
echo $x."<br />";
?>
Okay, the function is called 'for'
We passed 3 arguments to the functions, first:
PHP:
$x = 1;
We are declaring that X equals to 1
Second is argument, that works exactly as 'if' the loop weill be repeated until this 'if' won't be TRUE.
The last argument is what happens every repeat, $x++ means that every repeat 1 will be added to $x
$x++ - Add 1
$x-- - substract 1
I know it might be hard to understand, but don't worry I didn't understand this function first time as well It's a bit complicated.
After running this code you should see on your website:
1
2
3
4
5
6
7
8
9
10
The secod argument is:
PHP:
$x <= 10;
So the loop will be repeated until $x is 10.
There is also second loop, it more easier, it's called 'while'
Let's see an example.
PHP:
<?
$x=1;
while($x <= 10){
echo $x."<br>";
$x++;
}
?>
This code will show exactly same thing as the 'for' example
In this function you just give one argument, it's exactly as 'if' the loop will be repeated until this argument is TRUE
So, first we declare $x = 1;
So $x equals to 1.
Then we make 'while' which will be repeated until $x equals to 10 or it's smaller.
And the functions inside loop is showing actuall $x value, and then add's 1 to it.
Return of this code should be:
1
2
3
4
5
6
7
8
9
10
Also, there is one more loop called 'Do... while' but I won't be telling you about it, because it's not so inportant and in my experience, I never used it.
But still you can read about it here:
PHP: do-while - Manual
Some times we need to stop repeating in loop and start it again, to do this we need to add function
PHP:
continue;
Example:
PHP:
<?
for($x = 1; $x<=100; $x++) {
if($x % 2 != 0)
continue;
echo $x." ";
}
?>
In this example, the return will be the even numbers.
If you don't remember from last lesson % returns the rest of dividing, so if the rest of dividing by 2 is 0 then it must be even number
Also, sometimes you need to exit the loop, to do this you use function called:
PHP:
break;
Example:
PHP:
<?
for($x = 0; $x<10; $x++) {
if($x%2==0)
echo $x." ";
if($name == "Test1")
break;
}
?>
Now, we are going to learn about 'Switch' it does the same job as 'if' but it's written differently.
Let's see an example:
PHP:
<?
$i = 3;
switch($i){
case 0:
case 1:
case 2:
case 3:
echo "Variable i is smaller or same as 3";
break;
case 4:
echo "Variable i is equal to 4";
break;
default:
echo "Variable i is bigger than 4";
}
?>
Okey, let's explain this.
First we declare variable: $i which is equal to 3.
Now we make Switch and the only one argument we give is the name of variable.
The 'case' as the name says tells us what we do when $i is this case.
PHP:
case 0:
case 1:
case 2:
case 3:
So, if the
$i is 0 then go to next case
$i is 1 then go to next case
$i is 2 then go to next case
$i is 3 then go to next case
$i is 4 then print text "Variable i is smaller or same as 3"
Then we declare function break; which finish this block of 'case'
Then we declare next case: 4
So if the $i is 4 text: "Variable i is equal to 4" will be printed.
We declare break; again and now function 'default' works same as 'else' in 'if' this block will be executed only if no case has been executed for example, if the $i is 5.
Now we are going to learn about tables, this is very usefull function if PHP, and it's very easy to handle data.
For example, you want to give info about your car to variables, until now you would do it like this:
PHP:
$mycarcolor = "blue";
$mycarenginesize = "2.0";
etc.
or
PHP:
$car_color = "blue";
But there is better way to do it.
One name of the variable might handle all this data.
Let's see an example.
PHP:
$car['color'] = "blue";
$car['enginesize'] = "2.0";
So now, if you want to call it you use this for example:
PHP:
echo $car['color'];
It's much better and it's very usefull for example in writing configs, example:
PHP:
$config['dbhost'] = "localhost";
$config['dblogin'] = "root";
$config['dbpass'] = "";
$config['dbname'] = "tut";
Now, we are going to learn one of the most important things in PHP, and one of the most usable
FUNCTIONS! We are going to write our own functions.
First, we gonna try something easy.
Example:
PHP:
function mytext()
{
echo "My Text";
}
We declare new function by function 'function'
then we give name of functions and brackets, in there we give arguments, but our functions is easy and we don't need any, then we write what our function will do, in this case will print text "My Text"
if you want to declare you own function now, just write:
PHP:
mytext();
Because this is your name of functions, and now everytime you will call this function text "My Text" will be shown.
Let's write function with arguments.
PHP:
<?
function calculator($num1, $num2)
{
return $num1+$num2;
}
echo calculator(1, 4);
?>
Now, as you can see when we declare new function we gave two arguments, so now when you declare this function in your code you need to pass them.
So now, if you call function you need to type in brackets your variables, in this case I just used numbers:
PHP:
echo calculator(1, 4);
So, now function will add 1+4
But why did I use echo? Why I just don't used this function like that one before? Because in this function I didn't use echo but I used 'return' this is new function that you don't know yet, it returns the value but I doesn't display sometimes it's important on website, because you don't want to display the text but do something else with it.
Now, it's time for your homework.
1)
Make a loop for me that will show only ODD numbers from 1 to 100.
2)
Make for me function called calculator, where I will be able to give 3 arguments.
First number, Second number, type of calucalion
example
PHP:
caluclator(1, 5, "divide");
Or
PHP:
caluclator(1, 5, "add");
Don't forget to leave me rep point