PHP Basics


Standard Tags <?php   ... code  ?>
Short Tags <?... code    ?>
<?= $variable ?>which allows you to print the result of an expression directly
to the script’s output.
Script Tags <script language=“php”>... code</script>
ASP Tags <%... code%>//deprecated

TO PRINT:   echo    printf,     sprintf, print_r, var_dump()
NOTE:echo is not a function and, as such, it does not have
a return value.
Difference between print and echo
print"String 1", "String 2";//Language Construct

if ((print "foo") && (print "bar"))
echo"Hello"

comments in php  //    # /*                                                   

*/   
/**
* API Documentation Example
*
* @param string $bar
*/
function foo($bar) { }


<?
$x=pow(2,16);//65536
$x=pow(2,32);//4294967296 signed number proof system is 64 bit
$x=pow(2,64);//1.844674407371E+19 Not Handled by int4byte

echo $x;
?>



VARIABLE VARIABLE   (isset,unset)

$x=null;
echo isset($x);//False

Special use of  curly Braces
$name="1ashish";//1ashish becomes variable but variable cannot start vith number
$$name="mittal";
echo ${'1ashish'};//To resolve problem, we have to use { }

Note:Variable variables are a very powerful tool, and should be used with extreme care, not
only because they can make your code difficult to understand and document, but also
because their improper use can lead to some significant security issues.



 superglobal variables  $n= urldecode($_SERVER['QUERY_STRING']);

    

Typecasting:  settype($a,’string’’), gettype(),

intval(),   (int)$value,

$x=  ((0.1+0.7)*10);
echo $x;//8
$x=  (int)((0.1+0.7)*10);//7.9999999
echo $x;//7


$res=(bool)(0);//False
$res=(bool)(1);//True
$res=(bool)("");//False
$res=(bool)("0");//False
$res=(bool)("00");//True
$res=(bool)("x");//True


ESCAPE SEQUENCE:\n   \r \$ \” \\ \t  \{ \[

header('Content-Type:text/plain');

Avoiding the need to escape quotes with heredoc syntax
form in html :get, post, request,



<?
$left="ABD";
$right="ABC";
echo $left>$right;//True

?>
<?
$left="apple";
$right="Apple";
echo $left<$right;//False

?>
<?
$left="apple";
$right="Apple";
echo $left>$right;//1

echo (int)($left>$right);//1

?>

bitwise(&,|,!,)
<?
$x=0;
echo ~$x;//-1
$x=1;
echo ~$x;//-2

?>




• Assignment Operators for assigning data to variables
• Arithmetic Operators for performing basic math functions

<?
$x='aashish';
$y="mittal";
$res=$x+$y;
echo $res;//0
?>





• String Operators for joining two or more strings
  • Shift operator

<?
$x=1;
$x=$x<<1;
echo $x;//2

$x=1;
$x=$x<<32;//Try
echo $x;//1
$x=$x<<64;//Try
echo $x;//1
?>


• Comparison Operators for comparing two pieces of data
• Logical Operators for performing logical operations on Boolean values
In addition, PHP also provides:
• Bitwise Operators for manipulating bits using boolean math
• Error Control Operators for suppressing errors
@mysql_connect();//will not show any error like mysql_connect deprecated etc.
• Execution Operators for executing system commands
   backtick operator

$a = ‘ls -l‘;//Linux
echo `dir`;//Windows

• Incrementing/Decrementing Operators for incrementing and decrementing
<?
$x='aashish';
echo $x;  //aashish
echo ++$x;//aashishi
echo ++$x;//aashishj

?>
<?
$x='aashish';
echo $x;  //aashish
echo --$x;//aashish
echo --$x;//aashish

?>

numerical values
• Type Operators for identifying Objects

concat(.),comma(,);   increment decrement (++,--)    $fare Type = $age > 16 ? 'adult' : 'child';

type-juggling  (Handle with care)

<?php
$str1="123aaa";
if($str1==123){//PHP silently convert string to integer first
   echo "Hello";//Hello
}
if($str1===123){//=== is solution
   echo "Bye";//Nothing
}

?>

start basic of html forms[htmlentities|striptags()]



Reactions:

0 comments:

Post a Comment