PHP Notes

Block Spam Using PHP Code

PHP Note begin

First thing need to know that is the phpinfo() function, after you setup the server, how could you test the server can process php script or not? You can create a file like this


< ?php
	phpinfo();
?>

then open the file using your browser, then the page will display all your server environment information.
or you can just simply echo a string to display 😛


< ?php
	echo ("Hello World");
?>

That can do it too 😀
——————————————————————————————————————————-

Loops

When learning a programming language, some basic loop need to know
They all look pretty much the same {nearly} (C#, VB, .NET, Java, PHP) < - I've learned

1. Do – Wile Loop

Syntax

$i = 0;
$n2 = 0;
do{
  echo "The number is " .$n2."
"; $n2++; $i++; }while($i < 10);

2. While Loop

Syntax

$i = 0;
$n2 = 0;
while ($i < 10){
 echo "The number is " .$n2."
"; $n2++; $i++; }

3. For loop

Syntax

$n2 = 1;
for($i = 0; $i < 10; $i++){
  echo "The number is ".$n2."
"; $n2++; }

3.1 For Each loop for array in PHP

Syntax

$x = array(1,3,5,7,9);
foreach ($x as $val){
  echo $val."
"; }

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

Statements

And then most important {if else} statement

1. If Else Statement

Syntax

$i = 8;
if ($i == 0){
  $i = 0;
}
else{
  $i = 8;
}

2. Switch Statement

Syntax

switch ($x){
  case 1:
    echo "Number 1";
    break;
  case 2:
    echo "Number 2";
    break;
  case 3:
    echo "Number 3";
    break;
  default:
    echo "Other Number";
}

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

Functions

Syntax




< ?php
	function doSomething(){
		echo "Inside do something function";
	}
	echo "I am in ";
	doSomething();
?>


Function with parameters





< ?php
function writeName($fname){
	echo $fname . " Refsnes.
"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?>

Function with Return values





< ?php
	function add($x,$y){
		$total=$x+$y;
		return $total;
	}

	echo "1 + 16 = " . add(1,16);
?>



-------------------------------------------------------------------------------------------------------------------------------
Continue with
Array Page