• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

PHP Paxton's Web Development School Part: 2

Paxton

Banned User
Joined
Feb 23, 2008
Messages
4,110
Reaction score
48
Location
London, UK
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:

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 ;)
 
That's not exactly "table", but in PHP its called "array". Also it would be good if you would declare the variable as an array:
Code:
$var = array();
before writing it elements. ;)

Whats the disadvantage from Lua tables (as many people may be confused)?
In lua tables you may declare constants (ex. 1), key (ex. 2) and autokey (ex. 3)
Code:
local table = {bla = "lol"}
table.bla
Code:
local table = {"bla" = "lol"}
table["lol"]
Code:
local table = {"lol"}
table[1]
while in php you may use only key and autokey
Code:
$var = array("lol" => "lol");
$var['lol']
Code:
$var = array("lol");
$var[0]
also, Lua tables start with value 1, while php (whats actually standard...) with 0.
 
Last edited:
That's not exactly "table", but in PHP its called "array". Also it would be good if you would declare the variable as an array:
Code:
$var = array();
before writing it elements. ;)

Whats the disadvantage from Lua tables (as many people may be confused)?
In lua tables you may declare constants (ex. 1), key (ex. 2) and autokey (ex. 3)
Code:
local table = {bla = "lol"}
Code:
local table = {"lol" = "lol"}
Code:
local table = {"lol"}
while in php you may use only key and autokey
Code:
$var = array("lol" => "lol");
Code:
$var = array("lol");
also, Lua tables start with value 1, while php (whats actually standard...) with 0.

Don't u think its a bit confusing for begginers?
 
I was bored and made the homework lol xD
1) Loop ODD numbers
PHP:
<?PHP
while($x < 100) 
{
 $x++;
   if ($x % 2 != 1)
   {
      continue;
   }
   echo $x."<br>";
}
?>


2) Calculator
PHP:
<?PHP
function calculator($num1, $num2, $type) {
	if ($type == "sum") {
	return $num1+$num2;
	}
	else if ($type == "div") {
	return $num1/$num2;
	}
	else if ($type == "rest") {
	return $num1-$num2;
	}
	else
	{
return "Select a valid type (sum, div, rest)";
}
}
?>
 
I was bored and made the homework lol xD
1) Loop ODD numbers
PHP:
<?PHP
while($x < 100) 
{
 $x++;
   if ($x % 2 != 1)
   {
      continue;
   }
   echo $x."<br>";
}
?>


2) Calculator
PHP:
<?PHP
function calculator($num1, $num2, $type) {
	if ($type == "sum") {
	return $num1+$num2;
	}
	else if ($type == "div") {
	return $num1/$num2;
	}
	else if ($type == "rest") {
	return $num1-$num2;
	}
	else
	{
return "Select a valid type (sum, div, rest)";
}
}
?>

Well done :)
 
Loop ODD
PHP:
<?php
for ($x = 1; $x <= 100; $x++)
{
if ($x % 2 != 0)
continue;
echo $x. "<br>";
}
?>

Calculator
PHP:
<?php
function calculator1 ($num1,$num2)
{
return $num1+$num2;
}
echo calculator1 (1,5);
function calculator2 ($num3,$num4)
{
return $num3/$num4;
}
echo calculator2 (1,5);
function calculator3 ($num5,$num6)
{
return $num5*$num6;
}
echo calculator3 (1,5);

?>

Let the 3th part come =D
 
PHP:
<form action="php.php?action=calc" method="post" autocomplete="off">
	<input type="text" maxlength="5" size="3" name="fN"/>
	<input type="text" maxlength="5" size="3" name="sN"/>
	<select name="t">
		<option>Sum</option>
		<option>Minus</option>
		<option>Multiply</option>
		<option>Divide</option>
		<option>Raising</option>
	</select>
	<input type="submit" value="Go!"/>
</form>

<?
	$action = $_GET['action'];
	$fN = $_POST['fN'];
	$sN = $_POST['sN'];
	$t = $_POST['t'];
	
	function calc($number, $number_, $type)
	{
		switch($type):
			case "Sum":
				$outcome = $number + $number_;
				break;
			case "Minus":
				$outcome = $number - $number_;
				break;
			case "Multiply":
				$outcome = $number * $number_;
				break;
			case "Divide":
				$outcome = $number / $number_;
				break;
			case "Raising":
				$num = $number;
				for($o = 1; $o < $number_; $o++)
					$num *= $number;
					
				$outcome = $num;
				break;
		endswitch;
		
		return round($outcome, 2);
	}
	
	if($action == 'calc'):
		if(!empty($fN) && !empty($sN) && is_numeric($fN) && is_numeric($sN))
			echo 'Calculator says: '.calc($fN, $sN, $t);
		else
			echo 'Calculator says: GTFO!';
	endif;
?>

The first task is nab, so I did the calc only. PLEASE PASS :(:(
 
PHP:
<form action="php.php?action=calc" method="post" autocomplete="off">
	<input type="text" maxlength="5" size="3" name="fN"/>
	<input type="text" maxlength="5" size="3" name="sN"/>
	<select name="t">
		<option>Sum</option>
		<option>Minus</option>
		<option>Multiply</option>
		<option>Divide</option>
		<option>Raising</option>
	</select>
	<input type="submit" value="Go!"/>
</form>

<?
	$action = $_GET['action'];
	$fN = $_POST['fN'];
	$sN = $_POST['sN'];
	$t = $_POST['t'];
	
	function calc($number, $number_, $type)
	{
		switch($type):
			case "Sum":
				$outcome = $number + $number_;
				break;
			case "Minus":
				$outcome = $number - $number_;
				break;
			case "Multiply":
				$outcome = $number * $number_;
				break;
			case "Divide":
				$outcome = $number / $number_;
				break;
			case "Raising":
				$num = $number;
				for($o = 1; $o < $number_; $o++)
					$num *= $number;
					
				$outcome = $num;
				break;
		endswitch;
		
		return round($outcome, 2);
	}
	
	if($action == 'calc'):
		if(!empty($fN) && !empty($sN) && is_numeric($fN) && is_numeric($sN))
			echo 'Calculator says: '.calc($fN, $sN, $t);
		else
			echo 'Calculator says: GTFO!';
	endif;
?>

The first task is nab, so I did the calc only. PLEASE PASS :(:(

Ok, well done, but use $PHP_SELF in your form action, because I need to change the name of file to run it :)

Just a tip :p
 
Part 1:
PHP:
<?php 

for($x = 1; $x<=100; $x++) { 
if($x % 2 == 0) 
continue; 

echo $x."<br>"; 
} 

?>
This one was too easy all I had to do was change ! to = in your script for displaying evens only xD


Part 2:
PHP:
<?php

function calculator($num1, $num2, $tool) {
if ($tool == "add") {
return $num1+$num2;
}
else if ($tool == "subtract"){
return $num1-$num2;
}
else if ($tool == "multiply"){
return $num1*$num2;
}
else if ($tool == "divide"){
return $num1/$num2;
}
else{
echo "Invalid math type";
}
} 

echo calculator(1, 4, "multiply");

?>

Good? :eek:

Really enjoying the tutorials :thumbup: Nice job!

@down nao :(

but here's another one using switch
PHP:
<?php
function calculator($num1, $num2, $tool) {
switch($tool) {
case "add":
return $num1+$num2;
break;
case "subtract":
return $num1-$num2;
break;
case "multiply":
return $num1*$num2;
break;
case "divide":
return $num1/$num2;
break;
default:
echo "Invalid math type";
}
}
echo calculator(1, 4, "add");

?>
 
Last edited:
Simple question:

PHP:
$x++;
Can it be wrotten as:
PHP:
$x = $x + 1;

If no how I can add ex. 2 to $x in every loop?

##
PHP:
<?php
$x = 1;
while($x <= 100): 
if($x % 2 == 0)
echo $x."<br/>";
$x = $x + 1;
endwhile;
?>
 
Last edited:
Back
Top