Mindmajix

PHP Introduction and Variables, Operators in PHP

PHP

  • Rasmus Lerdorf is the founder of the PHP in 1995.
  • PHP is the server side scripting language.
  • Php needs a server to excute the file.
  • Php files should be saved with .php as its extension “.php3”, or “.phtml”
  • Or else our server will not excute unknown extension apart from .html, .cpi, .cgs,
  • Php support all the data base . that present in the market

Ex:-mySql,mssql,infonix,Sybase,ibmdb2,postgresql………etc

  • Php can be worked on all the operating systems that is windows,linux,max,solaries…etc
  • Php can communicate with different servers

Ex:-SMTP(Simple mail transfer protocal),imap (internet max access protocal), LDAP, Informix, Oracle, Sybase, Solid, PostgreSQL

  • PHP  can be install as a software tags we get many of the server files

Ex: -Xampp, Wamp, easy php, mamp

  • PHP stands for PHP: Hypertext Preprocessor
  • PHP is an open source software
  • PHP is free to download and use
  • PHP is easy to learn and runs efficiently on the server side

Xampp: – xampp stands for

x—————————–>platform(windows,linux,mac,solaries)

a—————————–>apache(web server)

m—————————->mysql(database file)

p—————————->perl(perl scripting in php)

p—————————->php/my admin (web database tool)

php current version is 5.3.8

 

wamp: – wamp stands for

w—————————->window exv

a—————————–>apache(web server)

m—————————->mysql(database file)

p—————————->php/my admin (web database tool)

Mamp: – mamp stands for

m—————————–>mac ‘os’

a—————————–>apache(web server)

m—————————->mysql(database file)

p—————————->php/my admin (web database tool)

Apache contain the  1-65535 ports                                1-1024 pre-reserved ports

Tem: – temp holds session details & temp upload files

Htdocs: – after installing xampp we have to store our files into the folder called htdocs. Once files are saved in htdocs we can open with the location path as

http://localhost/abc(folder name)/first.php

 

echo:-echo is the output method in php

BASIC PHP SYNTAX:-

<?php

echo “universal way of writing php<br>”;

?>

<script language  = ‘php’>

echo “javascript way of writtting php<br>”;

</script>

<? echo”short tag way of writing php<br>”; ?>

<% 1echo “asp way of writing php<br>”;  %>

COMMENTS IN PHP:-

In PHP, we use // to make a one-line comment or /* and */ to make a comment block:

<html> <body>
<?php //This is a comment
/* This is a comment block  */
?>
</body> </html>

PRINTING METHODS IN PHP:-

Echo:-echo as the arguments can print multiple arguments. Echo as a function can print only one argument

Print: -print as an argument can print only one argument.print also a function

Ex: – print”arg<br>”;

Printf: – printf will take formatting string with respected values as print

Ex: –printf(“(int%d) (float%f) (string %s) (asci%c)”);

Print-r: – print-r will print array of string

Ex: – $g = array (10,20,30,true,’kalam’);

Print-r($g);

Var-dump: –it is also printing method which is used debugging the errors very easily. This not used in production level (live environmental)

Ex: – var-dump($g);

<h1>Printing Methods in PHP</h1>

<?php  $a = 10; $b = 20; $c = 30;

echo “Arg01″,”Arg02″,”Arg03<br>”;

echo $a,$b,$c,'<br>’;

echo (“Arg01<hr>”); print “Arg01<br>”; print(“Arg01<hr>”);

printf(“(int %d) (float%.2f) (string %s) (Ascii %c) “,22.25,10.2545,’Rajesh’,97);

echo ‘<hr>’;  $g = array(10,20,30,true,’Rajesh’);

print_r($g);   echo ‘<hr>’;

var_dump($g);  ?>

CONTROLLING:-

Ex: – controlling.php

<style>     b{color:green;}  </style>

<?php

echo “line 01<br>”;

echo “line 02<br>”;

echo “line 03<br>”;

?>

<b>This is Bold Tag</b><hr>

<?php

if(false){          echo “<b>The 1<sup>st</sup> is Executed<br></b>”;     }

else {         echo “<b>The 2<sup>nd</sup> is Executed<br></b>”;    }

?>

<hr>

<h2>Alternate Method</h2>

<?php

if(true){

?>

<b>The 1<sup>st</sup> is Executed<br></b>

<?php

}

else

{

?>

<b> The 2<sup>nd</sup> is Executed<br></b>

<?php }  ?>

VARIABLES

  • Variables are “containers” for storing information.
  • Variables in PHP starts with a $ sign, followed by the name of the variable
  • The variable name must begin with a letter or the underscore character
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
  • A variable name should not contain spaces
  • Variable names are case sensitive (y and Y are two different variables)

Ex: -variables.php

<h1>Variables in php</h1>

<?php

$a = 10;

$name = ‘Rajesh’;

$_name

$_name

$n123

$_1232

$1_name

$first_Name

echo ‘The Value is : ‘,$a.'<br>’;

echo ‘The Name is : ‘,$name,'<br>’;

?>

<script>

var a = 15;

document.write(‘The Value of a is : ‘+a+'<br>’);

</script>

  • In PHP, a variable does not need to be declared before adding a value to it.
  • In the example above, notice that we did not have to tell PHP which data type the variable is.
  • PHP automatically converts the variable to the correct data type, depending on its value.
  • In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.

 

PHP has four different variable

  1. local
  2. global
  3. static
  4. Super global array of variable

1) LOCAL VARIABLE:-

A variable declared within a PHP function is local and can only be accessed within that function.

Ex:- localvariable.php

<?php

function test(){

$a = 10;

echo ‘The Value of $a inside function is : ‘,$a,'<br>’;

}

test();

echo ‘The Value of $a Outside  Function is : ‘,$a,'<br>’;

?>

2) STATIC VARIABLE:-

When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted.

To do this, use the static keyword when you first declare the variable.

Ex: -staticvariable.php

<?php

function test(){

$a = 10;

static $b = 20;

echo ‘The Value of $a inside function is : ‘,$a,'<br>’;

echo ‘The Value of $a inside function is : ‘,$b,'<br>’;

$a++;   $b++;

}

echo ‘The Value of $b outside is : ‘,$z,'<br>’;

test();

test();

?>

3) GLOBAL VARIABLE:-

Global scope refers to any variable that is defined outside of any function.

Global variables can be accessed from any part of the script that is not inside a function.

To access a global variable from within a function, use the global keyword:

Ex: – global.php

<?php

$x = 10;  echo ‘The Value of $x = ‘,$x,'<br>’;

function test(){           //global $x;

echo ‘The Value of $x inside Function = ‘,$x,'<br>’;

echo ‘$GLOBALS[“x”] = ‘,$GLOBALS[‘x’],'<br>’;              }

test();   ?>

4) SUPER GLOBAL ARRAY OF VARIABLE:-

$global is the super global array variable. This is the pre-defined keyword. In php we have mainly some of the super global variables. This can be used with their keywords.

i) $_EVN: – this is used for getting the information of your operating system ($_ENV (PATH))

ii) $_SERVER: – will give you the information about your server(apache information)

iii) $_GET: – it is used for form processing through get method (or) query string

iv) $_POST: –this is used for form processing through post method in secure manner

v) $_REQUEST: –will send the form through both get and post and cookie

vi)$_COOKIE: – this is used to get the information about the browser who is using the

web(computer)

vii)$_SESSION: –it is used to get the information of the user in between login and logout details are store at server machine

OPERATORS

1. Assignment operator:    = is used to assign values to variables in PHP

2. Arithmetic operator:         +,-,/,*,%

3. Increment & decrement operator:     i++,++I  & i–, –i

4. Comparison operator:      ==,!= ,===

5. Relational operator:<=, >=, <>

6. Logical operator:&&, ||, and, or

7. Error handling operator: –

8. scope resolution & this operator:    ::—–>scope,   &——->this

 

Ex: -operator.php

<?php

echo ‘<h1 align=”center”>Assignment Operator</h1>’;

$a = 10;

echo ‘The Value is : ‘.$a.'<br>’;

echo ‘The Value is Added by 2 : ‘,$a += 2,'<br>’;

$name = ‘Rajesh’;

echo ‘My name is : ‘.$name,'<br>’;

$name .= ‘ Kumar’;

$name = $name.’ Kumar’;

echo ‘My name is : ‘,$name,'<br>’;

echo ‘<h1 align=”center”>Arthamatic Operator</h1>’;

echo ‘The value of 2+3 : ‘,2+3,'<br>’;

echo ‘The Value of 2-3 : ‘,2-3,'<br>’;

echo ‘The Value of 2/3 : ‘,2/3,'<br>’;

echo ‘The Value of 2*3 : ‘,2*3,'<br>’;

echo ‘The Value of 2%3 : ‘,2%3,'<br>’;

echo ‘<h1 align=”center”>Increment & Decrement [INC/DEC]</h1>’;

echo ‘<h2>POST & PRE increment</h2>’;

$x = 10;

echo ‘The Value of $x = ‘,$x,'<br>’;

echo ‘The Value of $x++ = ‘,$x++,'<br>’;

echo ‘The Value of $x = ‘,$x,'<br>’;

echo ‘The Value of ++$x = ‘,++$x,'<br>’;

echo ‘The Value of $x = ‘,$x,'<br>’;

echo ‘<h2>POST & PRE Decrement</h2>’;

echo ‘The Value of $x– = ‘,$x–,'<br>’;

echo ‘The Value of $x = ‘,$x,'<br>’;

echo ‘The Value of –$x = ‘,–$x,'<br>’;

echo ‘The Value of $x = ‘,$x,'<br>’;

echo ‘<h1 align=”center”>Comparision Operator</h1>’;

var_dump(10 == 10);echo'<br>’;

var_dump(10 != ’10’);echo'<br>’;

echo ‘<h1 align=”center”>Relational Operator</h1>’;

var_dump(10<=10);echo ‘<br>’;

var_dump(10<>’10’);echo ‘<br>’;//not Equal to

echo ‘<h1 align=”center”>logical Operator</h1>’;

var_dump(true && true);echo ‘<br>’;

var_dump(true && false);echo ‘<br>’;

var_dump(true || true);echo ‘<br>’;

var_dump(true || false);echo ‘<br>’;

var_dump(false AND FALSE);echo ‘<br>’;

var_dump(false OR FALSE);echo ‘<br>’;

echo ‘<h1 align=”center”>Error Handling Operator</h1>’;

echo @sqrt();

//@test();

echo ‘Some Data’;

echo ‘<h1 align=”center”>Scope Resolution & this Operator {:: & ->(this)}</h1>’;

class A{

static $z = 100;

public $a = 20;

public $k = 50;

protected $b = 30;

private $c = 40;

function test(){

echo “A Test Function called inside Class A”;

}

}

$obj = new A;

echo $obj->a,'<br>’;

echo $obj->k,'<br>’;

//echo $obj->b,'<br>’;

//echo $obj->c,'<br>’;

echo ‘ The Static Value is : ‘,A::$z,'<br>’;

echo ‘The Function inside Class is : ‘,$obj->test(),'<br>’;

?>

TYPE CASTING:-

Conversion of the one data type into another data type is known as type casting. in php we have mainly two types of casting

1. Temporary type casting

2. Permanent type casting

1. Temporary type casting: – casting which can be done for single step by pre-defined the data type keyword. That particular variable will hold Temporary type casting

Ex : – temporary.php

<?php

$x = “10.254Kgs”;

var_dump($x);echo “<br>”;

var_dump((int)$x); echo “<br>”;

var_dump($x); echo ‘<br>’;

var_dump((float)$x); echo ‘<br>’;

var_dump((boolean)$x);echo'<br>’;

var_dump((string)$x);echo “<br>”;

var_dump($x);echo “<br>”;

var_dump(floatval($x)); echo ‘<br>’;

var_dump(intval($x)); echo ‘<br>’;

var_dump($x);echo “<br>”;

?>

 

2. Permanent type casting: – this can be done by using pre-defined key-word set type.which hold two arguments

Syntax: – set type (var name,’int’);

Ex: – Permanent type casting.php

<?php

echo ‘<h1>Parmanent Type Casting</h1>’;

$x = “10.254Kgs”;

settype($x,’float’);

var_dump($x);echo “<br>”;

settype($x,’int’);

var_dump($x);echo “<br>”;

settype($x,’boolean’);

var_dump($x);echo “<br>”;

settype($x,’int’);

var_dump($x);echo “<br>”;

var_dump($x);echo “<br>”;

settype($x,’float’);

var_dump($x);echo “<br>”;

?>

 

TYPE JUGGLING:-

conversion of data types by using expression to another data types are  known as type juggling

Ex: -typejuggling.php

<?php

echo ‘true + true : ‘,true+true,'<br>’;

if(null){           echo “Printed”;            }

else {          echo “Not Printed”;     }

?>

VARIABLE VALIDATION FUNCTION:-

i) Isset: –Isset of variables will check with the variable initialize are not and return Boolean

ii) empty(var name): – empty function will check the variable are not and return Boolean

iii) Unset (var name): –this function will remove the variable (or) delete the variable (or) initialize variable

iv) get type(var name): – this function will let us known the argument the data type in the form of string .this should be used because the coming variable this would be depreciated

Ex: -variable_validation.php

<?php

$x = 10;

var_dump($x);echo ‘<br>’;      var_dump(isset($x));    var_dump(isset($a));echo ‘<hr>’;

$z = ”;

var_dump(empty($z));   var_dump(empty($y));

$k = 10;                     echo ‘<br>’;                  var_dump(gettype($k));

unset($x);                  var_dump(isset($x));

?>

0 Responses on PHP Introduction and Variables, Operators in PHP"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.