For those starting on PHP...

Questions about programming languages and debugging
Post Reply
User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

For those starting on PHP...

Post by Gogeta70 »

PHP Reference File - Gogeta70

I got really really bored so i just wrote this for fun, hope it is useful.


Rand() - generates a random number between minimum and maximum numbers specified.

ex:

Code: Select all

rand(1, 10)
would generate a number between one and ten.

_____

strpos() - used to find something in a string - case-sensitive

ex:

Code: Select all

$string = "Mary had a little lamb";
$find = "little";

$result = strpos($string, $find)

//if "little" was found, then $result would = 1, however, if it did not find it, $result would = 0.
//so if you wanted to check if it = zero, you would type:

if($result === 0) /* notice the 3 = signs? */
{
echo $find . " was not found.";
} else {
echo $find . " was found.";
}
_____

strstr() - find the first occurance of a letter/number/special character in a string.

ex:

Code: Select all

$string = "Mary had a little lamb";
$find = " a";

$result = strstr($string, $find)

echo $result; 
//this would return everything after the character found, if the character was found, so it would have output:
//"a little lamb"
//this is also a case sensitive type library function.

_____

stripos() - is the same as strpos() (see above) but case-insensitive.

_____

stristr() - same as strstr() (see above) - case-insensitive

_____

strlen() - get the length of a string.

ex:

Code: Select all

$string = "Mary had a little lamb";
$length = strlen($string)

echo "The string is " . $length . " character(s) long.";
this would echo "The string is 33 character(s) long." - this always includes spaces.

_____

strrev() - reverse a string.

ex:

Code: Select all

$string = "Mary had a little lamb";

$reversed = strrev($string);

echo $reversed;
this would print out: "bmal elttil a dah yraM"

_____

strtr() - translates certain specified characters

ex:

Code: Select all

$string = "Màry hàd à little làmb";
$trans = strtr($string, "à", "a");

echo $trans;
This wuold output : "Mary had a little lamb"

_____

substr() - returns all characters between specified ending and beginning characters

ex:

Code: Select all

$string = "Mary had a little lamb";

echo substr($string, 0,4) // outputs: Mary
echo substr($string, 5, 3) outputs: had
_____

md5() - encrypt a string into a md5 hash.

ex:

Code: Select all

$string = "Mary had a little lamb";
$encrypted = md5($string);

echo $encrypted;
this would output: e946adb45d4299def2071880d30136d4

_____

md5_file() - md5 a specified file

ex:

Code: Select all

/* assume mary.txt includes "Mary had a little lamb" */

$file = "mary.txt";
echo md5_file($file);
_____

Explanation: Arrays.

Arrays are useful for many things, like holding lots of information in one variable, therefore, less variables
for you to remember. Often arrays are used in php flat file databases when someone does not wish to use an sql or other type of database. For instance, a user/password database.

heres an example:

Code: Select all

$user = array();

$user("a", "b", "c")

/* to get all information out of one of the values in the array, you would type: 
echo $user[0] because the first element in array is counted as 0, and not 1. */

echo "List of users:<Br><Br>";

echo $user[0] . "<Br>";
echo $user[1] . "<br>";
echo $user[2] . "<Br>";

/* that would be a long and tedious task for someone that had alot of users in that array, so that's where we came
up with: count(ARRAY) which counts all values in the array here's an example of it's usage. */

echo "Current number of users in database: " . count($user);
now i will tell you about the while loop, which will give me the ability to show you how to make showing the users
in the array, easier.

_____

while() {} - used to loop code until broken.

ex:

Code: Select all

echo "List of users in database:<BR><BR>";
$a = 0;
while($a < count($user))
{
echo $user[$a] . "<Br>";
$a++;
}
Much easier, huh?

_____

if() {} - used to compare

ex:

Code: Select all

$a = 1;
$b = 1;
$c = $a + $b;
if($c == 2)
{
echo "The variable "$c" does equal 2!";
} else {
echo "The variable "$c" does not equal 2.";
}
_____

for() {} - most complex loops in PHP. They behave like their C counterparts. - quoted off of http://php.net

ex:
Note: This is using variables from the array explanation above.

Code: Select all

for($a = 0; $a < count($user); $a++)
{
echo $user[$a] . "<br>";
}
for more help with this kind of loop: http://us3.php.net/manual/en/control-structures.for.php



-------------

This guide was not copied off of any site, and was written in my own free time by me alone, any information used off of a site was given credit to that site.

- Gogeta70
¯\_(ツ)_/¯ It works on my machine...

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Nice ^^
"The best place to hide a tree, is in a forest"

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Post by Gogeta70 »

Thanks.
¯\_(ツ)_/¯ It works on my machine...

Chaos1986
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 412
Joined: 03 May 2006, 16:00
17
Location: United States Of America
Contact:

Post by Chaos1986 »

Thank You Gogeta :) This Is Very Helpful Now I Have Something Easy To Read And Study On PHP. :twisted: :evil:
If Man Made It Man Can Crack Or Hack It & If You Want To Be A True Hacker You Need To Keep Your Mind Open And Always Be Willing To Learn
[img]http://img384.imageshack.us/img384/9996/chaos19862ub.png[/img]

Post Reply