Saturday, October 1, 2011

File Operation in PHP (Part 2)

Assalamualaikum and Selamat Sejahtera

Today we will learn these : fopen(),    feof()    ,fgets()    ,fclose().

1.Use the previous post text file.

2.Open the "www" folder,make a new PHP file called : openfile.php.Inside this file,write this code below.



3..Save it and run this file ,by type at the browser's address : http://localhost/openfile.php.


4.So,as we run it it will display this on the browser :

What we usually expect it,it will display the content of file.But it only display this words.For your information,this words are the pointer for phpFILE.txt. So,how to display it?well we will use the next function which is fgets(). Here is the code.

So,before run this file.Open the phpFILE.txt first and then add another this statement :


Okey,now run it....and of course la,the browser will display like this :

So,the function that we will discuss is feof().The meaning of feof is "file end of file".So,in while loop,it will keep asking the function "file end of file" either its already end of file yet.Function "file end of file" will be true when it reach at end of file.That why if we type while like this : while( feof ($file_to_handle) ) ,it will only show the end line of file only.

Furthermore,fopen(first parameter , second parameter) function for to open the file.The first parameter is for the name of file.The second parameter is for mode ,"r" is means read.Here is example of modes:


ModesDescription
rRead only. Starts at the beginning of the file
r+Read/Write. Starts at the beginning of the file
wWrite only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
aAppend. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+Read/Append. Preserves file content by writing to the end of the file
xWrite only. Creates a new file. Returns FALSE and an error if file already exists
x+Read/Write. Creates a new file. Returns FALSE and an error if file already exists


That all for today
Thank you!

Friday, September 30, 2011

File Operation in PHP



Assalamualaikum and Selamat Sejahtera

1.Make a text file called phpFILE.txt.Inside that text file,just type "This is PHP file.You open this file!".

2.Then put this textfile inside the folder called "www".

3.Inside the "www" folder,make a new PHP file called : readfile.php.Inside this file,write this code below



4.Save it and run this file ,by type at the browser's address : http://localhost/readfile.php

Beside,there is another function same as readfile("nameofFILE.txt") which is called file_get_contents(the_file_that_to_be_read).Here is the example of code:



That all for today,Thank You!!

Sunday, May 29, 2011

Security PHP part(3)

Assalamualaikum and selamat sejahtera

As we already learn at previous post ,we only learn on function that just straightly print out the any special characters or code.But,unfortunately that function is not that dynamic because maybe some special characters or code,we think that it is not harmful.So,for this post we will learn a function that enable us to select any characters and this function will read the selected the characters instead of straightly print......Word is not good as i show the picture,ryte?So,below i show to you,the function that i talk about.

The function is:  strip_tags($string,tags-that-to-ignore);

So,here is the situation,we want the power up the security that we want to block any tags that might harmful such as anchor tag(<A HREF="aaa"></A>) that link to any website.But,for bold tag(<B></B>) is not a harmful tag ryte...it just bold some text.So,to make this situation happen.First,we copy first the previous code ,then find this line:

$name = $_POST['name'];
echo $name;

and change it to this:

$name = $_POST['name'];
$name = strip_tags(
$name, "<B>");
echo $name;
Run it,and type this code below inside the textbox ,then click the 'send'  button:

<B>Hye,i'm a bold text</B>

it should display right this,ryte?
 


   That all for today!!any question??just ask here

N/nOtE:So,we can unblock any tag that we want,just only one type of tag only.

Saturday, May 28, 2011

Security PHP part(2)

Assalamualaikum and selamat sejahtera

Wow,its been a long time that i stop to post.Well,something happen must have some reason to cause  it happened ,right?huhuhu...the reason is i'am quite busy recently.doing some homework..like sweeping the leaves,throw some thrash,reading comic(Shin Chan),sleeping and some jobs that i think it quite reasonable to do,haha.Okey,just stop this some nonsense things,now we just continue what we have not done yet.

To follow this post please refer the previous post first.

There is a lot of ways to tackle this attack,so,for this post,we will only learn the two function to practice the security.

So,the first function is htmlspecialchars().Here is the example:

First copy this code:

 <html>
<head>
<title>Security PHP</title>



<?PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['name'];

echo $name;
}


?>

</head>
<BODY>

<Form Method = "Post" action ="SecurityinPHP.php">
<input type = "text" name = "name" value ="Test full name">
<input type="submit" name="Submit" value="send">
</Form>

</BODY>
</html>


 and then,try to change this line:

$name = $_POST['name'];
echo $name;
 

to this:

$name = $_POST['name'];
$name = htmlspecialchars($name);
echo $name;

 Then,run your code and type this code below into the textbox,then click "send" button:

"<A HREF ="dummysite">A Bad/Virused Site</A>"
  

it should display right this ryte:

 So,it means that the textbox does not longer read any special characters or any code anymore just like the previous post.It just straightly print the code or any special characters without read it.It quite safe right.But how about if the attacker use the different language like Russia,Germany,Arabic and others than English.Do not worry we have another flexible function that called htmlentities().To test it,just replace it with htmlspecialchars()  at the code.

Thank you,that's all for today,See you next time,hahaha


N/NOte :i'm more prefer on htmlentities() rather that htmlspecialchars().If you got something to ask or to add,just comment here..^^

Sunday, April 17, 2011

Sunday, March 20, 2011

HOW-TO-INCLUDE-FILE-PHP

Assalamualaikum and selamat sejahtera

Morning!!hope u in good condition or in happy mood...waaa its been a long.....time that i stop posting,huhu..whatever it is...let we start with the new technique that enable us to include a file by using a PHP code.Example,u got a php code and then u want to include some file that content php code too!!wow it sound something new!!ryte2??hahaha..shall we start...off we go!!!

so,we a file that named myFile.txt

inside myFile.txt type:

this is from myFile.txt

close myFile.txt.After that,here is PHP code:

<HTML>
<HEAD>
<TITLE>Include files</TITLE>
</HEAD>

<BODY>
<H3>Normal text here </H3>
This text written in HTML

<H3>Include File here</H3>

<?PHP include "myFile.txt" ; ?>

</ BODY>
</ HTML >

test it!!

okey,then edit myFile.txt.open it and change that text to be like this:


<?PHP

function PrintFromFile( ) {
print "This function from the myFile.php";
}


?>

then save n close it,go to code, search this line:

<?PHP include "myFile.txt" ; ?>  

change it,to be like this:

<?PHP include "myFile.txt" ;
PrintFromFile()
?>

explain:note that,inside the PHP code,we add the name of function that from included file that all.Okey run it!


N/NOTE:anything problem???

Tuesday, March 1, 2011

Header Function PHP

Assalamu'alaikum and selamat sejahtera

Have you ever wonder that a line of code that enable us just go the other website.I meant before this,we just run the php file then it just display as white background and what we can see form address column it just write "http://localhost/...".So,how us to run or execute the php file and then automatically change the address column to any website that we desire.Here is the code:
 
<?PHP
header("Location: http://www.google.com");
?>

run it!so it go to google page ryte?automatically!

N/NOTE:wah!!so fun!!hehe

Special Post:$_SERVER

Assalamu'alaikum and selamat sejahtera

As i know la,we just had 6 post for creating function,well this post it is just for knowledge.ok ?hahaha,well its quite boring if we just had the same title post everyday!So,imagine that you are an admin and then you want to know the information of server and a little bit on visitor like which website the visitor come from,what browser the visitor used.

Here is the code:

<?PHP

foreach($_SERVER as $name => $value) {
print $name . " = " . $value . "<br>";
}

?>

N/NOTE:don't expect me high!on this post....i just know a little bit only...huhuhu,i just know to display that all...but we can learn!!hahaha

Create-new-function-PHP (Part 6)

Assalamu'alaikum and selamat sejahtera

So,now we will learn on the other alternative of the return.Before this,we must type "return" in order the function to return the value to any variable that come from the function.So,we just use one character only!to enable the return value.Here is the example:


<?PHP

$number1 = 120;
calculate_addition($number1);

print $number1;
 
function calculate_addition(&$number1){
$number2 = 10;


if ($number1 > 100) {
$number1=$number1+$number2;
}
 

else {
$number1= $number1-90;
}

}

?>

we just use "&" as the alternative of the return.Compare this code from previous post:

<?PHP
$number1 = 120;
$total = calculate_addition($number1);

print $total;
function calculate_addition($number1){
$number2 = 10;

if ($number1 > 100) {
$number1=$number1+$number2;
}

else {
$number1= $number1-90;
}

return $number1;
}

?>
 


N/NOTE:&!!!!!!

Create-new-function-PHP (Part 5)

Assalamu'alaikum and selamat sejahtera

So,for this post we will learn on how to get value from the function,before we already learn on how display the value of variable from the function.Ok,here is the code:


<?PHP

$number1 = 120;
$total = calculate_addition($number1);
 

print $total;
 
function calculate_addition($number1){
$number2 = 10;


if ($number1 > 100) {
$number1=$number1+$number2;
}
 

else {
$number1= $number1-90;
}
 

return $number1;
}

?>

So for this post,let me explain the new word,"return".the concept is simple is that it is for assigning any variable from outside the function.In our case,we use $total to get the value from the return.So,we just got the value from the function.



 N/NOTE:the function,calculate_addition return the value ,and then $total are the variable that receive the value from the function.Proof:$total = calculate_addition($number1); .Any question?

Monday, February 28, 2011

Create-new-function-PHP (Part 4)

Assalamu'alaikum and selamat sejahtera

For this post,we will learn on how to create a new function that can detect the space or blank space from application.Well,we already learn the trim() but for this function,we want to display some error message if the user did not put anything but still press the submit button.Here is the example:


<?PHP
$user_input = trim("Ali Abu ");
detect_blank($user_input);
function detect_blank($user_input) {
if ($user_input == "") {
print "Sorry,you forget to fill the textbox";
}
else {
print "The form is OK";
}
}

?>

ok,you run it..it print "The form is OK" ryte?so how about change this line,just assuming that $user_input is get the value from the form/application that fill by the user.So change this line:


$user_input = trim("Ali Abu ");
to this:


$user_input = trim("");
so,it print "Sorry,you forget to fill the textbox" ryte?

that's all for today!adios!

N/NOTE:new function for this hectic morning!

Sunday, February 27, 2011

Create-new-function-PHP (Part 3)

Assalamu'alaikum and selamat sejahtera

For this post,we will learn on how to past the variable that declared that outside the function to the our function,than that function will process it and come out with output.Here is the example:


<?PHP
 
$display_text = "Variable message";
display_message($display_text);

function display_message($display_text) {
print $display_text;
}


?>

or how about we apply some mathmatic:


<?PHP
$number = 2;
display_math($number);

function display_math($number) 
{
$number=$number+4;
print $number;
}

?>

it print 2 or 4 or 6?so,i leave that answer to you. How about you just try to create a function that can accept multiple variable.Here is the example of function that can accept more than one variable::

function display_subtraction($number1,$number2)

try it!




N/NOTE:any problem?just comment here ^^

Create-new-function-PHP (part 2)

Assalamu'alaikum and selamat sejahtera

For this post we will try some function to display the variable.Ok,here is the example:

<?PHP
$display_text = "variable Detetceted";
display_message( );
function display_message( ) {
print $display_text;
}

?>

Run it,it print error ryte?so we call it and already declared the variable "$display_text" and yet we still can not display that variable in the function.So,as the conclusion,the function must have its variable itself ,then it can display its variable.what i meant is like this:

<?PHP
display_message( );
function display_message( ) {
$display_text = "display message";
print $display_text;
}

?>

that all....
N/NOTE:how about we declare a variable inside the function.....then directly display that variable,i meant display it without calling the function..try it!taste it! 
 

Saturday, February 26, 2011

Introduction to String function PHP(part 2)

Assalamu'alaikum and selamat sejahtera

Here is the example of the string function in PHP.

str_replace()

$text = "The string function";
$THEwordTHATyouWANTtoCHANGEit = "string";
$REPLACEthatWORDwithTHIS = "strong";

print $text . "<BR>";
$text = str_replace($THEwordTHATyouWANTtoCHANGEit,
$REPLACEthatWORDwithTHIS
, $text);
print $text;

str_word_count()

$number_words = str_word_count("how many word?");
print $number_words;

strlen()

$length_of_string = strlen("This is how to count the length of string");

-note:it count the character and the space in the string.

substr()

$username = "my name is dotcom";
$check = substr($username, strlen($username) - 6);

if ($check == "dotcom" ) {
print "ends in dotcom";
}
else {
print "doesn't end in dotcom";

-note:strlen($username)-6 :PHP will take six character because we put(-6), if -4 then it take 4 character that behind this string.So,our string is this :my name is dotcom,then PHP will start to count at red,bold 'm' as one until six.So sixth charatcer is:d . Then,$check is equal to 'dotcom'.


N/NOTE:selamat mencuba!!!

 

Create-new-function-PHP

Assalamu'alaikum and selamat sejahtera

On this post,we will learn the new function.So,to create new function we just type "function" with name that we desire like this :

function name_that_you_desire(){

print "this is my function!";

}

so,take this code or make your own function and put it inside PHP tag:
<?PHP
function name_that_you_desire(){
print "this is my function!";

}
?>

then run it...nothing happen ryte?its just like the other function..that we need to call it.In this case,we call it:

name_that_you_desire();


<?PHP
name_that_you_desire();

function name_that_you_desire(){

print "this is my function!";

}

?>

or


<?PHP
function name_that_you_desire(){
print "this is my function!";

}
name_that_you_desire();

?>

N/NOTE:custom function...

Introduction to String function PHP(part 1)

Assalamu'alaikum and selamat sejahtera

Here is the example of String function in PHP
----->TRY IT!<----------

chr()


$email = "nuar" . chr(64) . "Email.com";
print $email;

ord()


$asciiNumber= ord("%");
print $asciiNumber

echo()


$Alternativedisplay = "the other option to display";
print $Alternativedisplay;
echo $Alternativedisplay;

similar_text()

$real_name ="John lennon";
$guess_name = "John lenon";

$check_answer = similar_text($real_name, $guess_name, $percentCorrect);
print($check_answer) . "<BR>";
print($percentCorrect . "% correct");


//tell the accuracy of $guess_name as compare to $real name in percentage 
 
str_repeat()

$extra_alias = str_repeat("@", 5);
print $extra_alias;


click here for more on ASCII number 
 

Friday, February 18, 2011

Escaping technique-PHP

Assalamu'alaikum and selamat sejahtera

Copy this one and run it:

<?PHP
$escape = 'Alex's pencil';
print $escape;

?>

it print error ryte?it is because PHP is confuse because this variable got three single quotes ,So,we need to remove one of  it but we still want to display "Alex's pencil" on the browser.So change this line :
$escape = 'Alex\'s pencil';

no error ryte? it is because,this variable got  this ' \ '.This ' \ ' is called escape(as i understand la..),so it tell the PHP to skip one character only after it.After ' \ ' is single quote ryte?So PHP skip it..so PHP said that variable got two single quote only!

N/NOTE:  $escape = "Alex\'s pencil"; use double quote also can maa,hahaha

How-to-join-text-in-PHP script (part 2)

Assalamu'alaikum and selamat sejahtera

Okey....we already learn the joining thing in last post ryte...if you did not remember,just click here.but in this post,we will do on array..here is the example:


$group = array("Alpha", "Beta", "Charlie", "Delta");


and then we join it using implode():


$group=implode(",", $group);


the concept is same with explode()  that is,it need two thing: the separator and the variable name(but in array).
then we print it:

print $group;

the code:

<?PHP
$group = array("Alpha", "Beta", "Charlie", "Delta");

$group=implode(",", $group);
 print $group;
?>


try this!

$group=implode("-", $group);

Or

$group=implode("#", $group);
 
Or

$group=implode(" ", $group);

N/NOTE:any question guys?

How-to-split-a-text--->PHP

Assalamu'alaikum dan selamat sejahtera

Selamt pagi!morning!ohayou!

So,for this post,we will learn on how to split a text in PHP ,here is the quick example:

$line="Group 1,200,250,150,100,700";

but we want print it to be like this:


Group 1
200
:250
150
100
700

So,here is the code:

<?PHP
$line = "Group 1,200,250,150,100,700";
$line = explode(",",$line);
print $line[0];
?>

okey,let see...we got the new code again..here is:
explode(",",$line);

 so,explode is function to split the variable...so,inside the round bracket,we got a comma and variable name.we know that variable name is used for reference but for comma is to tell the explode function,that in order to split the text,you must read until you find the comma,then the next character will be split.so far okay?
 another explanation...is order to use this function,you must have two thing...one separator,one is variable..so,in this case,we declare $line  like this:


$line = "Group 1  ,  200  ,   250  ,   150   ,   100  ,   700";

 
so,we got the variable and the separator is "," is comma because it separate the word "Group 1" from other number :200,250,150,100,700.so far okey?hahaha

but the browser print "Group 1" ryte?it is because when the variable "$line" is split (using explode() function,the $line will change to an array that why in order to print it,we must put "[ ]".So,to print all,we can use for loops or this method:


print_r($line);

Then change the declaration of $line to be like this:

$line = "Group 1,Jan:1,Feb:2,Mac:3,April:4,Total:5";

and then use print_r().Then try use for loops....test it!is really fun!haha

 N/NOTE:try it!

Thursday, February 17, 2011

How-to-know-what-type-of-browser-the-users-use

Assalamu'alaikum and selamat sejahtera

So,PHP also provide a function that detect the type of the browsers.Well,this function usually use by administrator or some organization,just to analysis what type of browser that mostly used.So,it is just a additional knowledge only.Who know,maybe this function can be very beneficial to us.

The code:

<?PHP
$browser = $_SERVER["HTTP_USER_AGENT"];
print $browser;
?>

N/NOTE:every knowledge....must been something useful to us. 

Wednesday, February 16, 2011

How-TO-search-a-letter-inside-a-string-variable-PHP

Assalamu'alaikum and selamat sejahtera

So,for this very nice morning,we will learn on how to find the position of a letter that inside a string variable.For example,give a string variable:"hi five",and then we want to know the position of 'h'.So,we use strpos(string_to_use, letter_to_find, start).Here is the code:


<?PHP
$slogan = "hi five";
$position = strpos($slogan, "h");
print $position;

?> 

So,start is optional either you want to use it or not.It will print '0' ryte?it is because PHP read the letter 'h' as '0',letter 'i' as '1' ,etc.PHP start with zero when to count.How about we change the letter 'h' to be 'H'...it print nothing ryte?it is because PHP can not find any 'H' in 'hi five'.Logically,'h' is just same with 'H'..lol..it is just different in lower case and upper case.Even a small different can make a different result in PHP.So watch out!Okey so for this case,we can use "===",triple equals,to print the false value(as PHP state that "there is no 'H' in 'hi five' ")  result.Here is the code:

<?PHP
$slogan = "hi five";
$position = strpos(
$slogan,'H');

if ($letter_position === false) {
print "Letter not found " ;
}
else {
print "Letter found";
}


?>


N/NOTE:finding someone??

How-to-shuffle-character-PHP

Assalamu'alaikum and selamat sejahtera

For this post,we learn on how to shuffle the character,here is the code:

<?PHP
$variable_name = 'pencil ';
$variable_name = str_shuffle($variable_name);
print $variable_name;
?>

str_shuffle()-will shuffle the variable value.

N/NOTE:how about change the value of  $variable_name-=12345,if you want to know more la....

Deal(trimming) with white blank-PHP

Assalamu'alaikum and selamat sejahtera

So,for this post,we will learn on how to deal with white blank on the input.Quite confusing right?okey,here is an example of event,sometime we input our name to the system like this " alexander " .So,from this quotation marks,we can see that there is an extra space at before and after the text.Besides,the PHP will count this extra space as part of the name,and of course you do not want your name got some extra space....ugh!its ugly lol,hahaha.here is the proof,,but before that,strlen() is use to count the length of the string....try this code:

<?PHP
$name_space=" alexander ";
$input_Count = strlen($name_space);
print $input_Count;
?>

it will print,11 ryte?but "alexander" got 9 letter ryte?.So,to settle this problem we just trim this name.trim()-it will tell the PHP,not to count the white blank as the letter.Here is the code:


<html>
<head>
<title>Lessons for Trimming space</title>


<?PHP

$name_space = trim(" alexander ");
$input_Count = strlen($name_space);
print $input_Count;


?>

</head>

<body>


</body>
</html>

  info
ltrim( )  -tell the PHP not to count space at beginning of a string
rtrim( )  -tell the PHP not to count space at end of a string

N/NOTE:hoooraay!!my name got no white blank anymore!!hahaha

Modify Case-PHP

Assalamu'alaikum and selamat sejahtera

So,for today we will learn on how to modify your string variable.For example,if we enter the input like this:"rock star" and we want the PHP to modify this input,to be like this :"Rock Star" or  "ROCK STAR".Okey,here is the code:

<html>
<head>
<title>Lessons for Changing Letter Case</title>


<?PHP

if (isset($_POST['Submit1'])) {

$full_name = $_POST['user_name'];
$full_name = ucwords($full_name);

print($full_name);

}

?>

</head>

<body>


<FORM NAME ="form1" METHOD ="POST" ACTION ="">

<Input type = 'text' Name ='user_name' value ="input name">
<P>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "enter">
</FORM>

</body>
</html>

Here is the explainatio,so the new code for this lesson is:

$full_name = ucwords($full_name);

So,ucwords() function will take the $full_name(make sure that this variable's type is string) as its parametre,and modify tthis variable.For example,$full_name="rock star",so the ucwords($full_name) will modify $full_name as this variable has become the input for this function,finally,$full_name="Rock Star".

Besides,we also another function:


$full_ name=strtoupper($full_ name);
$full_ name=strtolower($full_ name);
$full_ name = ucfirst($full_ name);


test this function!if you want to know more...


N/NOTE:lol

Monday, February 14, 2011

Count-function-PHP

Assalamu'alaikum and selamat sejahtera,

Today,we will learn on how to count the element from your array using the code.So,the function is called count.Below is the example and some explanation.

the example:

<html>
<head>
<title>Count function</title>

<?PHP
$squad = array("alpha", "beta", "charlie", "delta");
$arr_count = count($squad);
print ("the number of element from your array=".$arr_count);
print("<br>");

for ($Number = 0; $Number < $arr_count; $Number++)
{print ("the element= ".$squad[$Number]."<BR>");}

?>
</head>
<body>
</body>
<html>

the explanation :

$arr_count = count($squad);

so we a variable to count the element in our array.In this case,we use "$arr_count" as a variable to count the element inside the array.So,it need to be declare that is equal to count($squad).So that,the count function will assign the value to $arr_count.


N\NOTE:try it!...need any help?please comment here...

Tuesday, February 8, 2011

Use random in an array-PHP


Assalamu'alaikum and selamat sejahtera

For this post,we will discuss on "array_rand( the name of your array,total of random keys that you want)" function.For example:

<?PHP
$number = array(1 => 16 ,  2 => 13 ,  3 => 44 ,  4 => 74 ,  5 => 50 ,  6 => 63);
$random_numbers = array_rand($number, 1);
print $random_numbers;
?>


PHP will get the key name (what is key name?refer on previous post ).So it only print the key name only.To get the random value of  an array ,change this line:

print $random_numbers;


to be like this:


print $number[$random_numbers];


The code:


<html>

<head>

<title>Randoms-Array</title>

<?PHP

$number = array(1 => 16, 2 => 13, 3 => 44, 4 => 74, 5 => 50, 6 => 63);

$random_numbers = array_rand($number,1);

print $random_numbers;


?>

</head>

<body>

</body>

</html>


So,that's all

N/NOTE:any confusing??

Sorting Array

Assalamu'alaikum and selamat sejahtera

This post will discuss on how to sort our array.So,before this we already learn the foreach loops,it will print all the key name and key value.So,sometimes,we may want to sort it.It very simple,we just use some special function that is:


sort($fname_of_your_array);
  
another sorting function:

rsort( ) – Sorts a array in reverse order
arsort( ) - Sorts the alphabet array in reverse order
krsort( ) - Sorts the Keys in an array in reverse order

here some proof for asort():
figure 1: Before using asort()
figure 1.1: After using asort()


The code:

<html>

<head>

<title>Foreach loops Array</title>

<?PHP

$squad_name = array();

$squad_name["alpha"] = "wing";
$squad_name["beta"] = "tail";
$squad_name["charlie"] = "head";
$squad_name["delta"] = "backup";

asort($squad_name);

foreach ($squad_name as $squad_name => $squad_code) {
print "Squad Name = " . $squad_name . " Squad Code = " . $squad_code . "<BR>";
}

?>

</head>

<body>

</body>

</html>

N/NOTE:LOL


Array-For each loops

Assalamu'alaikum and selamat sejahtera

So,what is for each loops?okey for this post we will discuss it and practice it on our code.Here is the code:


$squad_name = array( );
$squad_name["alpha"] = "one";
$
squad_name["beta"] = "two";
$
squad_name["charlie"] = "three";
$
squad_name["delta"] = "four";

foreach ($squad_name as $key_name => $key_value) 
{
print "Key = " . $key_name . " Value = " . $key_value . "<BR>";
}


So,we just concentrate on this line first:


foreach ($squad_name as $key_name => $key_value) 

 so,REMEMBER : foreach NOT for each. Okey,so you put the name of the array,in our case,the name of array is $squad_name.Next is "as $key_name =>$key_value". the key name for this array is alpha,beta,charlie and delta.So,the value of key_name,alpha is one.Do you get the idea?.Besides,you also came named it any name that like this:

foreach ($squad_name as $team_code => $team_number

N/NOTE:next post,we will discuss more on application of this loops

Friday, February 4, 2011

How to get value from array-PHP

Assalamu'alaikum dan selamat sejahtera

So,as previous post,i already give the PHP script ryte?so,this post,we will discuss on that script.Here is the script

<?PHP

$start = 1;
$times = 2;
$answer = array();

for ($start; $start < 11; $start++) {
$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");

}

?>:

So,just focus on this line:

$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");

As $start=1,then it will be look like this : $answer[$start]=$answer[1] and then we assign the value in the array,like this:

$answer[1] = 1 * $times;

and then,to print or get the value from the array,we just need print($answer[1]); ,as in our case,this line below will get the value from the array:

print (" answer = " . $answer[1] . "<BR>");

after that it will loop again ,and $start will update and it's value will be 2.

Instead use number to get the value from array.we also can use text,like this:

$group = array();

$group["alpha"]="Team One";
$group["beta"]="Team Backup";

and to get the value:

print($group["alpha"]);

that all for this post.


N/NOTE:any confusing?

Array-PHP

Assalamu'alaikum and selamat sejahtera

Okey,for this post,we will learn the array.What is array?click here for more.So,first of all i show to you on how declare the array in PHP.

the Declaration:

                  $Group = array( );

So,$Group is because i named it,well you can put any variable's name that you like.But,the important is,$Group already be an array because of "=array()".So,to assign the value to the array,look at this example below :

$Group = array();

$Group[]="Eagle";
$
Group[]="Alpha";
$
Group[]="Beta";
$
Group[]="Charlie";

beside we also can do like this:
 
 $Group = array("Eagle","Alpha","Beta","Charlie");

okey,as we assign this,PHP will assign this values to be like this:

$Group[0]=Eagle
$Group[1]=Alpha
$Group[2]=Beta
$Group[3]=Charlie

but,i want to be like this:

$Group[1]=Eagle
$Group[2]=Alpha
$Group[3]=Beta
$Group[4]=Charlie

well,its look common ryte?as usual,when we start to count ,we always start with one ryte?so,to make like this,follow this code below:

$Group = array();

$Group[1]="Eagle";
$
Group[2]="Alpha";
$
Group[3]="Beta";
$
Group[4]="Charlie";

or

 $Group = array(1=>"Eagle",2=>"Alpha",3=>"Beta",4=>"Charlie");

one more thing,if you want assign number instead of number,well here is the code:

 $Group = array(12,34,65);

or

$Group = array();

$Group[]=22;
$
Group[]=45;

so,we do not need the double quote,just straight away type what number that do you want to assign.To study more on the array.study this script:


<html>
<head>
<title>Array</title>
<?PHP

$start = 1;
$times = 2;
$answer = array();

for ($start; $start < 11; $start++) {
$answer[$start] = $start * $times;

print (" answer = " . $answer[$start] . "<BR>");
}
?>
</head>
<body>
</body>
</html>


N/NOTE:so far.....okay?

Thursday, February 3, 2011

Break while PHP looping

Assalamu'alaikum and selamat sejahtera

So,we already covered for,while and do..while loop.But as we learned that the PHP will still to loop until it reach the limit or the condition is does not true,ryte?.hOw about if we want to get out of the loop although PHP does not reach the limit or the condition still true.Okey,to do that,we must have some special code to done this task,luckily that special code is only one word that is break.Here is how the break is work:

$Interrupts = true;
$number = 1;

while ($number < =10) 
{
 

print("number = " + $number + "<BR>");
if ($Interrupts = = true) 

break;
 

$counter++;
}


Just try this code and run it.

N/NOTE:Explore it by yourself!see ya!

While Loop and Do...While Loop

Assalamu'alaikum and selamat sejahtera

After For loops ,we will learn the While loop and Do..While loop.For those who already learned the C programming or other language programming might does not need explanation on this loops.For the beginner,just click here for more explanation.

oKEy,the concept of this loop is quite simple:


while (condition) {
code that to be executed if the condition is true
}


For example

$number = 1;
while ($number =< 10) {
print (" number = " . $number . "<BR>");
$number++;
}


and for Do....While loop:

do
statement
while (condition)


for example:

$number = 1;

do
{
print (" number = " . $number . "<BR>");
$number++;

}
while ($number =< 10);

Is quite simple ryte?why don't u try these loop on the previous exercise.

The Code for do...while loop:
<html>
<head>
<title>Do...While loop</title>

<?PHP
$number=1;

do
{

print (" number = " . $number . "<BR>");
$number++;

}
while ($number <= 10);
?>

</head>

<body>
</body>

<html>


N/NOTE:lalalala

Exercise For Loop

Assalamu'alaikum and selamat sejahtera

After we learn the concept and practical of For loops.So...why dont us do some activity,this activity is called MathForLoops,the objective or cause is that we try to apply the math operation in For loops.Well as previous post,we already apply it on update , $counter and $number ,if u remember.Ok,for this post,we will do math table like times,minus,add and divide.The hint is refer the image below...i use add table,and u will do the other table.okey?hehe

Input:

Result:

Code:

<html>
<head>
<title>An Addition Table Programme</title>

<?PHP
    $add = 2;

if (isset($_POST['SubmitOne'])) {

    $initial = $_POST['txtInitial'];
    $final = $_POST['txtFinal'];
    $add = $_POST['txtAdd'];


    for($initial; $initial <= $final; $initial++) {
        $answer = $initial + $add;
        print $initial . " added by " . $add . " = " . $answer . "<BR>";
    }

}

?>

</head>

<body>


<FORM NAME = formOne Method = "POST" Action = "forloops.php">

Start Number: <INPUT TYPE = Text NAME  = txtInitial SIZE = 5 value ="1">
End Number: <INPUT TYPE = Text NAME = txtFinal SIZE = 5 value ="10">
Add By: <INPUT TYPE = Text NAME = txtAdd SIZE = 5 value = <?PHP print $add; ?> >
<P>
<INPUT TYPE = Submit Name = SubmitOne VALUE = "Do Add Table">
<P>

</FORM>
</body>

<html>

File name: forloops.php

N/NOTE:Try ur self!

For loops-PHP

Assalamualaikum and selamat sejahtera

loops?what it means?well for those who already learn the programming might already know what it is,and for beginner,click here.

make a file named ForLoop.php 
Here the code: 

<?PHP
$counter = 0;
$number = 1;

for($number; $number < 11; $number++
{
$counter = $counter + 1;
print $counter . "<BR>"; 

}
?>

Here a little explanation on this script

for(initial; limit; update){
}

initial
is where you tell PHP the initial value of your loop. In other words, which number to start the loop?So we used this:

$number = 1

Like all variables, you can make up your own name like $iterate,well it all up to you.
You must set or assign the value of $number before the loop begins, like this:

$number = 1
for($number; $number < 11; $number++) {


Or:

for($number = 1; $number < 11; $number++) {

The result is the same – the start number for this loop is 1

Limit

We have to tell PHP when to end your loop or the limit of the loop.So, we’re setting PHP to keep round the loop while the value of the variable $number is Less Than 11.

for($number; $number < 11; $number++) {

When the value of $number is 11 or higher, PHP will stop the loop.

Update

The loops need the update of starting value,in this case,we use $number,so $number need to be add if we set $number=1 or minus if we set $number=10 and the its limit is $number>1.So that,we can update the counter and display its update,thus make us believe the loops is working well.So to update,we usually like this:
  
$number++

the double plus symbol (++) =increment (increase the value by one).
It’s just a short way of saying this:
$number = $number + 1

the double minus symbol(--)=decrement (decrease the value by one)
It’s just a short way of saying this:
$number = $number - 1

So the story of this looping is like this:
“Starting/Initially/$number at a value of 1, keep going round and round while the start/$number value is less than and not equal to 11. Increase the starting/$number value by one each time round the loop.”
  
One more trick

$counter = $counter + 1;
print $counter . "<BR>"; 


or we can use like this:

 $counter++;
 print $counter . "<BR>"; 

 N/NOTE:that's all folk! 

Tuesday, February 1, 2011

Checkboxes-PHP

Assalamualaikum and selamat sejahtera

From previous,we already learn the radio button and for this post,we will learn the checkboxes.For the script of checkbox is quite similar with radio button.First we view on PHP script first:


<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';

if (isset($_POST['SubmitOne']))
 {

if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
 if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}

}
?>

So,first we unchecked the obj1 and obj2.then we check to see if the Submit button was clicked:

if (isset($_POST['SubmitOne'])) {
}

Inside of this code,we still have another isset( ) function:

if (isset($_POST['obj1'])) {
}
this statement are checking if a checkbox was set it is because if we did not check if the checkboxes are set or not,we will deal with a lot of errors,worse is "Undefined" errors.

If the checkbox is ticked, it will return a value. So the isset( ) function will be true,then our code inside of the if statement gets executed:
if ($obj1 == 'male') {
$obj1 = 'checked';
}

.Here is the simple example...

"If the value inside of the variable called $obj1 is 'male' then change the status of obj1 to checked
The second of the if statements are the same – one for each checkbox on the form.

Ok,done for PHP script,ok for HTML form:

<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">

<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Boy
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Girl
<P>

<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>

Its quite similar ryte with our previous post,ryte?

 <Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male

Just a little change,that all..
Finally make the new file named CheckBoxes.php
The code:
<html>
<head>
<title>Lessons for Checkboxes</title>

<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';

if (isset($_POST['SubmitOne'])) {

if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];

if ($obj1 == 'male')
{$obj1 = 'checked';}
}

if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];

if ($obj2 == 'female')
{$obj2 = 'checked';}
}

}
?>


</head>

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">

<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Male
<P>

<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Female
<P>


<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>

</body>
</html>


N/NOTE: good luck!question?