Skip to content
gpawade edited this page Feb 11, 2018 · 1 revision

PHP

PHP: Hypertext Preprocessor.

version - PHP 5.6.16

PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.

Usage

  • hello world
	<html>
   	   <head>
	      <title>Hello World</title>
	   </head>
	   <body>

	      <?php echo "Hello, World!";?>

	   </body>
	</html>
  • PHP Markup tags
	<?php PHP code goes here?>
	
	<?	php code goes here ?>

	<script	language="php"> PHP code goes here</script>
  • PHP Configuration file (PHP.INI)

The PHP configuration file, php.ini, is the final and most immediate way to affect PHP's functionality. The php.ini file is read each time PHP is initialized.

  • PHP is case sensitive

Syntax

  • Canonical PHP tags
	<?php...?>
  • Short-open tags
	<?...?>

Note - this tag we need to enable from php.ini file.

  • Comment
	<?
   	# This is a comment, and
   	# This is the second line of the comment
   	
   	// This is a comment too. Each style comments only
   	print "An example with single line comments";
   ?>
  • Variable
	 $msg = "john";
    print "Welcome $msg";

Variable Type

Simple Data type

  • Integer
$int_var = 12345;
  • Double
$many = 2.2888800;
  • Boolean TRUE or FALSE

  • NULL

	$var = NULL;
  • String

Constant

	<?php
		define("MINSIZE", 50);
		
		echo MINSIZE;
		echo constant("MINSIZE"); // same thing as the previous line
	?>

Array

<?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value )
         {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value )
         {
            echo "Value is $value <br />";
         }
      ?>

Key value array

<?php
         /* First method to associate create array. */
         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
  		
		 $salaries['mohammad'] = "high";       
?>

GET POST

 	# Get - http://www.test.com/index.htm?name1=value1&name2=value2
	 # Get value
	 $name = $_GET["name"];
	 
	 
	 
	 # Get post value
	 $name = $_POST["name"]

$_REQUEST variable

$_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

<?php
   if( $_REQUEST["name"] || $_REQUEST["age"] )
   {
      echo "Welcome ". $_REQUEST['name']. "<br />";
      echo "You are ". $_REQUEST['age']. " years old.";
      exit();
   }
?>

include() function

The include() function takes all the text in a specified file and copies it into the file that uses the include function.

<?php
	<?php include("menu.php"); ?>
?>

require() function

So there is no difference in require() and include() except require() function generates a fatal error and halt the execution of the script if there is any problem.

PHP Function

<?php
         /* Defining a PHP Function */
         function writeMessage($num1)
         {
            echo "You are really a nice person, Have a nice time!";
         }
         
         /* Calling a PHP Function */
         writeMessage(123);
		 
		 
		 #passing argument by reference
		 function test(&$num)
		 {
			 $num += 5
	     }
		 $n =5;
		 test($n);
		 
		 
		 # setting default value to function
		 function test2( $num = NULL)
		 {
		 }
		 
		 
		 # Dynamic function call
		 
		 $fun_holder = "test2";
		 $fun_holder();
		 	 
		 
      ?>

Session

A session creates a file in a temporary directory on the server where registered session variables and their values are stored. The location of the temporary file is determined by a setting in the php.ini file called session.save_path.

<?php
   session_start();
   	
	   $_SESSSION['counter'] = "some value";
	   
  ?>