Php Tactics

$GLOBALS

$GLOBALS[ index ] is use to store all global variable in an array.it’s use to access the variable out of the specific function.

simply it’s working like java Static variable.Therefore if we want to use  a variable inside of he function we have to use $GLOBALS

CASE I:
<?php
$a = 5;
$b = 10;
function myTest()
{
global $a, $b;
$b = $a + $b;
}
myTest();
echo $b;
?> 
CASE II:
<?php
$a = 5;
$b = 10;
function myTest()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
myTest();
echo $b;
?> 



STATIC
Also we can use static keyword to make the same feature as GLOBALS
static $variable 





 CONCATENATION

usually we are using “ + “ to combine two words. but in php we are using some “ . ” Operator to combine two word.

<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?> 



ARRAYS

Arrays can be define  many ways in php.

CASE i:
$cars=array("Saab","Volvo","BMW","Toyota");
CASE ii:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
?> 




ASSOCIATIVE ARRAYS

we can pass some stored data with this array set.for example if anyone want to pass the users Id and address to database they can use associative Arrays.

CASE i:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
CASE ii:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
?> 



FOREACH LOOP

According to the array list count we can make a easy for loop with few line of code.Therefore the loop will count the number of index in Array.

<?php
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "<br />";
  }
?>

It will print the all value that array contain.



GET AND POST METHOD

This get and post method is just a pipe to transfer data to next page or to Database.There is no restriction that get method should use for getting data and post method should for posting data .This get and post depend on what we are defining in HTML forms.  and name of the input.

<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> 
  
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old! 
here if we use form method as post then we have  to call $_POST in php.Just we are changing the name of the Pipe. 
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.
$_REQUEST can be use for any of the methods get of post . 

No comments:

Post a Comment