• Welcome to ForumKorner!
    Join today and become a part of the community.

reGi's PHP Beginner Series Part 2

reGi

User is banned.
Reputation
0
reGi’s PHP Beginner Series Part 2

String Functions

You can use strlen() to find the length of a given string, you can also use strpos() to find the location of given text in a string.

Example:
PHP:
<?php

echo strlen(“reGi”);
//Outputs 4

echo strpos(“reGi”, “G”);
//Outputs 3

?>

This can be used to check if a user’s password is long enough etc.

Operators

Check http://www.w3schools.com/php/php_operators.asp, I see no reason to rewrite this as it’s already been laid out their very neat and tidily.

If/Else Statements

If statements can check conditions and do different code depending on if its true or not.

Example:
PHP:
<?php

$mystring = “Hello”;
$mystring2 = “Hello2”;

If($mystring == $mystring2)
echo “Not Echoed”;
elseif($mystring!=$mystring2)
echo “Echoed”;
else
echo “If both previous statements are false, this will be true”;

?>

If you want your If statement to execute more than one line of code if true you need to put Curly brackets ( { } ) around the code.

Example:
PHP:
<?php

$number = 1;

if($number==1)
{
	echo “Number = “.$number;
	$number = 2;
}
else
{
	echo “Number = “.$number;
	$number = 1;
}

?>

Switches

Switches can be used to check a strings value and execute code similar to an If statement.

Example:
PHP:
<?php

$number = 1;

switch ($number)
{
case 1:
	echo “Number is 1”;
	break;
case 2:
	echo “Number is 2”;
	break;
default:
	echo “Number is not 1 or 2”;
}

?>

Default is used if none of the cases are true. You need to use break so that the code doesn’t run into the next case.

Arrays

Arrays are used to store multiple values in one variable. Numeric arrays store values which can be referred to using the index the value is stored at. Index starts at 0. Associative arrays store values which can be referred to using an id. Multidimensional arrays can store multiple sub arrays.

Numeric Arrays Example:
PHP:
<?php

$numbers=array(“1”, “2”, “3”, “4”, “5”);

echo $numbers[‘2’];
//Would print out 3

?>

Associative Arrays Example:
PHP:
<?php

$number[‘One’] = “1”;
$number[‘Two’] = “2”;
$number[‘Three’] = “3”;

echo $number[‘Two’];
//Would print out 2

?>

Multidimensional Arrays Example:
PHP:
<?php

$items = array(“Drinks”=>array(“Coca Cola”, “Dr. Pepper”, “Pepsi”), 
“Food”=>array(“Pizza”, “Burger”, “Chips”);

echo “I want to drink “.$items[‘Drinks’][0].” and eat “.$items[‘Food’][2];
//Would print “I want to drink Coca Cola and eat Chips

?>

Once again, that concludes part 2 of the PHP Beginner Series. Part 3, which will probably be the last part of this series will be released soon.

Note: most code is a simplified version of W3Schools.com code, I recommend you check out their site for more PHP information.
 

Legend_mybb_import1694

Active Member
Reputation
0
Very nice guide that you've written. I'm starting to get the hang of this. :D
 

reGi

User is banned.
Reputation
0
Indelibleâ„¢ said:
Very nice guide that you've written. I'm starting to get the hang of this. :D

Thanks :D Glad about that ;) if you need any help PM me and i'll see what i can do :p
 

sidorak95

Member
Reputation
0
Wow, this is almost as good as Tizag's PHP tutorial, but shorter, and to the point. Thanks man!
 

reGi

User is banned.
Reputation
0
sidorak95 said:
Wow, this is almost as good as Tizag's PHP tutorial, but shorter, and to the point. Thanks man!

Thanks! Means alot :) Glad you enjoyed it :p
 
Top