Wednesday 29 May 2013

PHP Syntax and Basics

So now after you would have gone through the install of the IDE and web server we are now ready to start :)

 PHP Syntax

  • In PHP scripting bloc starts with <?php and end with ?> This is the basic PHP Syntax.
  • A PHP scripting block can be placed anywhere in the document. Some of the web servers like xampp also support shorthand for <?php which is <? for starting and ?>  for closing.Hence in such cases PHP Syntax becomes <? for start and ?> for end


Simple script to print My first PHP script on browser is :
<body>
<?php               //start PHP Syntax
echo "My first PHP script";
?>
</body>
</html>



  • The code in PHP must end with semicolon.The semicolon in PHP acts as separator and is used to distinguish two or more set of instructions. 
  • The echo command is one of the three most commonly use output statements use in PHP others being print and print_r. We would discuss print_r later but now lets focus on echo and print.
  • Both can be used to print on browser but the basic difference between them is that echo does not return a value but print always returns 1. Other major differences can be found here Difference .


How to Run your PHP code on browser


  • Once you write your script it becomes essential to run them on browser to test  them so in order to do so you can make a folder in  G:\xampp\htdocs (g drive in my case) or can directly save page in G:\xampp\htdocs.
  • Its better to make folder for whole project and save file there with a .php extension  and if you want to run a single page you can save it directly in mentioned htdcos folder with a .php extension .
  • In order to now see your output on browser open any of the standard browser i consider mostly Mozilla as it works best with xampp.
  • Now directly writing localhost in URL bar will open your XAMPP manager.
  • Now in order to view your file you will have to write localhost/filename (for single file stored in htdocs) or localhost/foldername/filename (for file stored in a folder in htdocs)

Comments in PHP
PHP supports comments similar to those of C and C++. 
  • For single line comment we use //
  • For multi line comment we use /* to start and a */ to end comment.

Variables in PHP 
Variables as known are the names uses to store values such as numbers and strings etc. 
  • All variables in PHP start with $ sign .
Syntax ::  $var_name=value;

Now lets see this through an example 



<?php
$txt="Hello World";
$abc= 12;
echo "$txt"."  "."$abc";
?>

Output






Variables Variables
This is one of the unique features of PHP which may appear same as use of addresses in C and C++. 
A variable variable takes the value of a variable an treats that as the name of the variable. eg 
<?php 
$a="hello";
?>

 Then if we again write the code as 

<?php
$a="hello";
$$a="world";
echo "$a"." "."$hello".'<br>'; //print 1st method
echo "$a"." "."${$a}";     //print 2nd method
 ?>

Then in this case $$a is similar to as to write $hello where $hello is a variable variable and both the methodologies of printing produce same output on browser. we use {} while using variable variable directly. as seen in the above example.

Variable Naming Rules 
  • Variable must start with _(underscore) or with a letter.
  • Variable can only contain alpha numeric characters and underscores.
  • Variable should not contain any spaces.

PHP as Loosely Typed Language
We must keep always in mind that PHP is loosely typed language. in PHP a variable does not need to be declared before being set. PHP automatically converts the variable to correct data type depending on how they are set. Unlike strongly typed languages you don't have to use datatypes to define type of the variables here.

No comments:

Post a Comment