SlideShare a Scribd company logo
http://programmerblog.net
Introduction to Functions and Arrays
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundamentals: Functions
 A set of constructs which allow the programmer to break up their code
into smaller, more manageable, chunks which can be called by name
and which may or may not return a value.
 function function_name (parameters) {
function-body
}
 function helloWorld(){
echo "HelloWorld"; //No out put is shown
}
By ProgrammerBlog.net http://programmerblog.net/
Functions: Passing Parameters
 Passing parameters by value
– function salestax($price,$tax) {
$total = $price + ($price * $tax
echo "Total cost: $total";
}
salestax(15.00,.075);
$pricetag = 15.00;
$salestax = .075;
salestax($pricetag, $salestax);
By ProgrammerBlog.net http://programmerblog.net/
Functions: Passing Parameters
 Passing parameters by reference
$cost = 20.00;
$tax = 0.05;
function calculate_cost(&$cost, $tax)
{
// Modify the $cost variable
$cost = $cost + ($cost * $tax);
// Perform some random change to the $tax variable.
$tax += 4;
}
calculate_cost($cost,$tax);
echo "Tax is: ". $tax*100."<br />";
echo "Cost is: $". $cost."<br />";
By ProgrammerBlog.net http://programmerblog.net/
Functions: Default Argument
Values
 Default values are automatically assigned to the argument if no other value is
provided
function salestax($price,$tax=.0575) {
$total = $price + ($price * $tax);
echo "Total cost: $total";
}
$price = 15.47;
salestax($price);
By ProgrammerBlog.net http://programmerblog.net/
Functions: Optional Arguments
 Certain arguments can be designated as optional by placing them at the end of
the list and assigning them a default value of nothing .
function salestax($price, $tax="") {
$total = $price + ($price * $tax);
echo "Total cost: $total";
}
salestax(42.00);
function calculate($price,$price2="",$price3="") {
echo $price + $price2 + $price3;
}
calculate(10,"", 3);
By ProgrammerBlog.net http://programmerblog.net/
Functions: Returning Values from
a Function
 You can pass data back to the caller by way of the return keyword.
 function salestax($price,$tax=.0575) {
$total = $price + ($price * $tax);
return $total;
}
$total = salestax(6.50);
 Returning Multiple Values
function retrieve_user_profile() {
$user[] = "Jason";
$user[] = "jason@example.com";
return $user;
}
list($name,$email) = retrieve_user_profile();
echo "Name: $name, email: $email ";
By ProgrammerBlog.net http://programmerblog.net/
Functions: Nesting Functions
 defining and invoking functions within functions
function salestax($price,$tax)
function convert_pound($dollars, $conversion=1.6) {
return $dollars * $conversion;
}
$total = $price + ($price * $tax);
echo "Total cost in dollars: $total. Cost in British pounds: "
. convert_pound($total);
}
salestax(15.00,.075);
echo convert_pound(15);
By ProgrammerBlog.net http://programmerblog.net/
Functions: Recursive Functions
functions that call themselves
function nfact($n) {
if ($n == 0) {
return 1;
}else {
return $n * nfact($n - 1);
}
}
//call to function
nfact($num) ;
By ProgrammerBlog.net http://programmerblog.net/
Functions: Variable Functions
 Functions with parameters on run time
function hello(){
if (func_num_args()>0){
$arg=func_get_arg(0); //Thefirstargumentisatposition0
echo "Hello$arg";
} else {
echo "HelloWorld";
}
}
hello("Reader"); //Displays"HelloReader"
hello(); //Displays"HelloWorld"
By ProgrammerBlog.net http://programmerblog.net/
Server Side Includes (SSI):
include() function
 Include function
 You can insert the content of one PHP file into another PHP file before the server
executes it, with the include() or require() function. (e.g. Header, Menu, footer)
 The include() function takes all the content in a specified file and includes it in the
current file
 include() generates a warning, but the script will continue execution
<html>
<body>
<?php include("header.php"); ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>
By ProgrammerBlog.net http://programmerblog.net/
Server Side Includes (SSI):
include() function
 <html>
<body>
<div class="leftmenu">
<?php include("menu.php"); ?>
</div>
<h1>Welcome to my home page.</h1>
<p>Some text.</p>
</body>
</html>
By ProgrammerBlog.net http://programmerblog.net/
Server Side Includes (SSI):
include() function
<html>
<body>
<div class="leftmenu">
<a href="/default.php">Home</a>
<a href="/tutorials.php">Tutorials</a>
<a href="/references.php">References</a>
<a href="/examples.php">Examples</a>
<a href="/about.php">About Us</a>
<a href="/contact.php">Contact Us</a>
</div>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
</body>
</html>
By ProgrammerBlog.net http://programmerblog.net/
Server Side Includes (SSI):
require() function
 Require function
require() generates a fatal error, and the script will stop
include() generates a warning, but the script will continue execution
<html>
<body>
<?php
require("wrongFile.php");
echo "Hello World!";
?>
</body>
</html>
 It is recommended to use the require() function instead of include(), because
scripts should not continue after an error.
By ProgrammerBlog.net http://programmerblog.net/
PHP Built In Functions
 Math functions
 http://www.w3schools.com/php/php_ref_math.asp
– pi() Returns the value of PI
– pow() Returns the value of x to the power of y
– rad2deg() Converts a radian number to a degree
– rand() Returns a random integer
– round() Rounds a number to the nearest integer
– sin() Returns the sine of a number
– sinh() Returns the hyperbolic sine of a number
– sqrt() Returns the square root of a number
– srand() Seeds the random number generator
– tan() Returns the tangent of an angle
– tanh()
– abs() Returns the absolute value of a number
By ProgrammerBlog.net http://programmerblog.net/
PHP Built In Misc. Functions
 Constants
– M_LN10 Returns the natural logarithm of 10 (approx. 2.302)
– M_PI Returns PI (approx. 3.14159)
– M_SQRT2 Returns the square root of 2 (approx. 1.414)
 Miscellaneous Functions
– Strlen Returns length of a string
– count() Returns the count of an array.
– Strtolower strtolower() to lower case.
– strtoupper() strtoupper() convert string to upper case
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments - Arrays
 Arrays are ordered collections of items, called elements
 Each element has a value, and is identified by a key that is unique to the array It
belongs to
 In PHP, there are three kind of arrays:
– Numeric array - An array with a numeric index
– Associative array - An array where each ID key is associated with a value
– Multidimensional array - An array containing one or more arrays.
– $a = array();
– $state[0] = "Delaware";
– $a = array (10, 20, 30);
– $a = array (’a’ => 10, ’b’ => 20, ’cee’ => 30);
– $a = array (5 => 1, 3 => 2, 1 => 3,);
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments - Arrays
 Numeric Arrays:
– A numeric array stores each array element with a numeric index.
– $cars = array("Saab","Volvo","BMW","Toyota");
– $cars[0]="Saab"; //2nd
way of declaring arrays
– $cars[1]="Volvo";
– $cars[2]="BMW";
– $cars[3]="Toyota";
– echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
 Associative Arrays
– An associative array, each ID key is associated with a value.
– $ages = array(«John"=>32, «Jane"=>30, «David"=>34);
– $ages[‘John '] = "32";
– $ages[‘Jane '] = "30";
– $ages[‘David '] = "34";
– $states = array (0 => "Alabama", "1" => "Alaska"..."49" => "Wyoming"); //numeric
– $states = array ("OH" => "Ohio", "PA" => "Pennsylvania", "NY" => "New York")
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments - Arrays
 Multidimensional Arrays
 Arrays of arrays, known as multidimensional arrays
 In a multidimensional array, each element in the main array can also be an array.
 $cars = array ( “Toyota"=>array ( “Corolla“, " Camry“, "Toyota 4Runner” ),
“Suzuki"=>array ( “Vitara” ),
“Honda"=>array ( "Accord“, “Sedan“, “Odyssey” ) );
 echo "Is " . $ cars [Toyota '][2] . " a member of the Toyota cars?“;
 Is Toyota 4Runner a member of the Toyota cars?
 $states = array (
"Ohio" => array ("population" => "11,353,140", "capital" => "Columbus"),
"Nebraska" => array ("population" => "1,711,263", "capital" => "Omaha")
)
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments – Printing Arrays
 print_r() and var_dump()
 var_dump
– var_dump() outputs the data types of each value
– var_dump() is capable of outputting the value of more than one variable
– var_dump($states);
– $a = array (1, 2, 3);
– $b = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
– var_dump ($a + $b); // creates union of arrays
– If 2 arrays have common elements and also share same string or numeric key will
appearance in result or output
 print_r
– print_r can return its output as a string, as opposed to writing it to the script’s standard
output
– print_r($states);
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments – Comparing -
Counting Arrays
 $a = array (1, 2, 3);
$b = array (1 => 2, 2 => 3, 0 => 1);
$c = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
var_dump ($a == $b); // True
var_dump ($a === $b); // False returns true only if the array contains the
same key/value pairs in the same order
var_dump ($a == $c); // True
var_dump ($a === $c); // False
 $a = array (1, 2, 4);
 $b = array();
 $c = 10;
 echo count ($a); // Outputs 3
 echo count ($b); // Outputs 0
 echo count ($c); // Outputs 1
 is_array() function : echo in_array ($a, 2); // True
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments – Flipping and
Reversing , Range
 array_flip()
– Exchanges all keys with their associated values in an array
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);
print_r($trans);
– Output = Array ( [1] => b [2] => c )
 array_reverse()
– Return an array with elements in reverse order
 array_values()
– array_values — Return all the values of an array
– $array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
– array_keys — Return all the keys of an array
 range()
– The range() function provides an easy way to quickly create and fill an array consisting
of a range of low and high integer values.
– $die = range(0,6); // Same as specifying $die = array(0,1,2,3,4,5,6)
By ProgrammerBlog.net http://programmerblog.net/By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments – Sorting Arrays
 There are many functions in PHP core that provide various methods of sorting
array contents.
 sort
– $array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
sort($array);
var_dump($array);
 asort //to maintain key association,
- $array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
asort($array);
var_dump($array);
 rsort,
 arsort // sorting an array in descending order
By ProgrammerBlog.net http://programmerblog.net/
PHP Fundaments – Arrays as Stack
and Queues
 Stack Last in First Out.
 array_push()
– $stack = array();
array_push($stack, ’bar’, ’baz’);
var_dump($stack);
 array_pop()
– $states = array("Ohio","New York","California","Texas");
– $state = array_pop($states); // $state = "Texas"
 Queues
 array_shift, Shift an element off the beginning of array
 array_unshift - Prepend one or more elements to the beginning of an array
$stack = array(’qux’, ’bar’, ’baz’);
$first_element = array_shift($stack);
var_dump($stack);
array_unshift($stack, ’foo’);
var_dump($stack);
By ProgrammerBlog.net http://programmerblog.net/
Super Globals
 Several predefined variables in PHP are "superglobals", which means
they are available in all scopes throughout a script. There is no need to
do global $variable; to access them within functions or methods.
 $GLOBALS
 $_SERVER
 $_GET
 $_POST
 $_FILES
 $_COOKIE
 $_SESSION
 $_REQUEST
 $_ENV
By ProgrammerBlog.net http://programmerblog.net/
Features – Super Globals
 $GLOBALS — References all variables available in global scope
 <?php
function test() {
$foo = "local variable";
echo '$foo in global scope: ' . $GLOBALS["foo"] . "n";
echo '$foo in current scope: ' . $foo . "n";
}
$foo = "Example content";
test();
?>
 $_SERVER -- $HTTP_SERVER_VARS [deprecated] — Server and execution environment
information
 <?php
echo $_SERVER['SERVER_NAME'];
?>
By ProgrammerBlog.net http://programmerblog.net/
Super Globals
 $_ENV -- $HTTP_ENV_VARS [deprecated] — Environment variables
 <?php
echo 'My username is ' .$_ENV["USER"] . '!';
?>
 $ip = $_SERVER['REMOTE_ADDR'];
By ProgrammerBlog.net http://programmerblog.net/
Super Globals
 Thank you for viewing this slide. Hope this is helpful for you.
 please visit our blog
http://programmerblog.net
Follow us on twitter
https://twitter.com/progblogdotnet
By ProgrammerBlog.net http://programmerblog.net/

More Related Content

What's hot (20)

Php
PhpPhp
Php
Rajkiran Mummadi
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
Jussi Pohjolainen
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
Ahmed Swilam
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
Geshan Manandhar
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
Ashish Chamoli
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
Mark Baker
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
tumetr1
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
Bradley Holt
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Muthuganesh S
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
Anonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Anonymous Functions in PHP 5.3 - Matthew Weier O’PhinneyAnonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Anonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Hipot Studio
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
Bozhidar Boshnakov
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
Ahmed Swilam
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
Geshan Manandhar
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
Ashish Chamoli
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
Mark Baker
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
tumetr1
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
Anonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Anonymous Functions in PHP 5.3 - Matthew Weier O’PhinneyAnonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Anonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Hipot Studio
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 

Viewers also liked (14)

[Php] navigations
[Php] navigations[Php] navigations
[Php] navigations
Thai Pham
 
Magento eCommerce And The Next Generation Of PHP
Magento eCommerce And The Next Generation Of PHPMagento eCommerce And The Next Generation Of PHP
Magento eCommerce And The Next Generation Of PHP
varien
 
01 sistem bilangan real
01 sistem bilangan real01 sistem bilangan real
01 sistem bilangan real
sri puji lestari
 
行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)
行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)
行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)
releaseey
 
Enterprise PHP (PHP London Conference 2008)
Enterprise PHP (PHP London Conference 2008)Enterprise PHP (PHP London Conference 2008)
Enterprise PHP (PHP London Conference 2008)
Ivo Jansch
 
Terrorism
TerrorismTerrorism
Terrorism
Tej Surya
 
Enterprise PHP (php|works 2008)
Enterprise PHP (php|works 2008)Enterprise PHP (php|works 2008)
Enterprise PHP (php|works 2008)
Ivo Jansch
 
telecom
telecomtelecom
telecom
AJU P.T
 
Autoankauf
AutoankaufAutoankauf
Autoankauf
Köln Autoankauf
 
Audacity
AudacityAudacity
Audacity
Cherese Collins
 
KRC Summer Intern Slideshow!
KRC Summer Intern Slideshow!KRC Summer Intern Slideshow!
KRC Summer Intern Slideshow!
janikim
 
St web pages & booklet
St web pages & bookletSt web pages & booklet
St web pages & booklet
SoundsTogether
 
Trek Miles
Trek MilesTrek Miles
Trek Miles
David Benoy
 
[Php] navigations
[Php] navigations[Php] navigations
[Php] navigations
Thai Pham
 
Magento eCommerce And The Next Generation Of PHP
Magento eCommerce And The Next Generation Of PHPMagento eCommerce And The Next Generation Of PHP
Magento eCommerce And The Next Generation Of PHP
varien
 
行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)
行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)
行政院簡報 性平處:我國性別平等推動成果及未來展望(懶人包)
releaseey
 
Enterprise PHP (PHP London Conference 2008)
Enterprise PHP (PHP London Conference 2008)Enterprise PHP (PHP London Conference 2008)
Enterprise PHP (PHP London Conference 2008)
Ivo Jansch
 
Enterprise PHP (php|works 2008)
Enterprise PHP (php|works 2008)Enterprise PHP (php|works 2008)
Enterprise PHP (php|works 2008)
Ivo Jansch
 
KRC Summer Intern Slideshow!
KRC Summer Intern Slideshow!KRC Summer Intern Slideshow!
KRC Summer Intern Slideshow!
janikim
 
St web pages & booklet
St web pages & bookletSt web pages & booklet
St web pages & booklet
SoundsTogether
 
Ad

Similar to Php my sql - functions - arrays - tutorial - programmerblog.net (20)

PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptxPHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
Chris Chubb
 
Introduction to PHP_ Lexical structure_Array_Function_String
Introduction to PHP_ Lexical structure_Array_Function_StringIntroduction to PHP_ Lexical structure_Array_Function_String
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
DIWE - Advanced PHP Concepts
DIWE - Advanced PHP ConceptsDIWE - Advanced PHP Concepts
DIWE - Advanced PHP Concepts
Rasan Samarasinghe
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptx
ShaliniPrabakaran
 
Php course-in-navimumbai
Php course-in-navimumbaiPhp course-in-navimumbai
Php course-in-navimumbai
vibrantuser
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
40NehaPagariya
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
Hassen Poreya
 
PHP-Part3
PHP-Part3PHP-Part3
PHP-Part3
Ahmed Saihood
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
Intoduction to php arrays
Intoduction to php arraysIntoduction to php arrays
Intoduction to php arrays
baabtra.com - No. 1 supplier of quality freshers
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
Bozhidar Boshnakov
 
PHP Cheat Sheet
PHP Cheat SheetPHP Cheat Sheet
PHP Cheat Sheet
নির্ঝর আনজুম
 
PHP-Cheat-Sheet.pdf
PHP-Cheat-Sheet.pdfPHP-Cheat-Sheet.pdf
PHP-Cheat-Sheet.pdf
hectoralvarogarzonbu
 
Php
PhpPhp
Php
Yoga Raja
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
Phpwebdevelping
PhpwebdevelpingPhpwebdevelping
Phpwebdevelping
mohamed ashraf
 
Ad

Recently uploaded (20)

Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 

Php my sql - functions - arrays - tutorial - programmerblog.net

  • 1. http://programmerblog.net Introduction to Functions and Arrays By ProgrammerBlog.net http://programmerblog.net/
  • 2. PHP Fundamentals: Functions  A set of constructs which allow the programmer to break up their code into smaller, more manageable, chunks which can be called by name and which may or may not return a value.  function function_name (parameters) { function-body }  function helloWorld(){ echo "HelloWorld"; //No out put is shown } By ProgrammerBlog.net http://programmerblog.net/
  • 3. Functions: Passing Parameters  Passing parameters by value – function salestax($price,$tax) { $total = $price + ($price * $tax echo "Total cost: $total"; } salestax(15.00,.075); $pricetag = 15.00; $salestax = .075; salestax($pricetag, $salestax); By ProgrammerBlog.net http://programmerblog.net/
  • 4. Functions: Passing Parameters  Passing parameters by reference $cost = 20.00; $tax = 0.05; function calculate_cost(&$cost, $tax) { // Modify the $cost variable $cost = $cost + ($cost * $tax); // Perform some random change to the $tax variable. $tax += 4; } calculate_cost($cost,$tax); echo "Tax is: ". $tax*100."<br />"; echo "Cost is: $". $cost."<br />"; By ProgrammerBlog.net http://programmerblog.net/
  • 5. Functions: Default Argument Values  Default values are automatically assigned to the argument if no other value is provided function salestax($price,$tax=.0575) { $total = $price + ($price * $tax); echo "Total cost: $total"; } $price = 15.47; salestax($price); By ProgrammerBlog.net http://programmerblog.net/
  • 6. Functions: Optional Arguments  Certain arguments can be designated as optional by placing them at the end of the list and assigning them a default value of nothing . function salestax($price, $tax="") { $total = $price + ($price * $tax); echo "Total cost: $total"; } salestax(42.00); function calculate($price,$price2="",$price3="") { echo $price + $price2 + $price3; } calculate(10,"", 3); By ProgrammerBlog.net http://programmerblog.net/
  • 7. Functions: Returning Values from a Function  You can pass data back to the caller by way of the return keyword.  function salestax($price,$tax=.0575) { $total = $price + ($price * $tax); return $total; } $total = salestax(6.50);  Returning Multiple Values function retrieve_user_profile() { $user[] = "Jason"; $user[] = "[email protected]"; return $user; } list($name,$email) = retrieve_user_profile(); echo "Name: $name, email: $email "; By ProgrammerBlog.net http://programmerblog.net/
  • 8. Functions: Nesting Functions  defining and invoking functions within functions function salestax($price,$tax) function convert_pound($dollars, $conversion=1.6) { return $dollars * $conversion; } $total = $price + ($price * $tax); echo "Total cost in dollars: $total. Cost in British pounds: " . convert_pound($total); } salestax(15.00,.075); echo convert_pound(15); By ProgrammerBlog.net http://programmerblog.net/
  • 9. Functions: Recursive Functions functions that call themselves function nfact($n) { if ($n == 0) { return 1; }else { return $n * nfact($n - 1); } } //call to function nfact($num) ; By ProgrammerBlog.net http://programmerblog.net/
  • 10. Functions: Variable Functions  Functions with parameters on run time function hello(){ if (func_num_args()>0){ $arg=func_get_arg(0); //Thefirstargumentisatposition0 echo "Hello$arg"; } else { echo "HelloWorld"; } } hello("Reader"); //Displays"HelloReader" hello(); //Displays"HelloWorld" By ProgrammerBlog.net http://programmerblog.net/
  • 11. Server Side Includes (SSI): include() function  Include function  You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function. (e.g. Header, Menu, footer)  The include() function takes all the content in a specified file and includes it in the current file  include() generates a warning, but the script will continue execution <html> <body> <?php include("header.php"); ?> <h1>Welcome to my home page!</h1> <p>Some text.</p> </body> </html> By ProgrammerBlog.net http://programmerblog.net/
  • 12. Server Side Includes (SSI): include() function  <html> <body> <div class="leftmenu"> <?php include("menu.php"); ?> </div> <h1>Welcome to my home page.</h1> <p>Some text.</p> </body> </html> By ProgrammerBlog.net http://programmerblog.net/
  • 13. Server Side Includes (SSI): include() function <html> <body> <div class="leftmenu"> <a href="/default.php">Home</a> <a href="/tutorials.php">Tutorials</a> <a href="/references.php">References</a> <a href="/examples.php">Examples</a> <a href="/about.php">About Us</a> <a href="/contact.php">Contact Us</a> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> </body> </html> By ProgrammerBlog.net http://programmerblog.net/
  • 14. Server Side Includes (SSI): require() function  Require function require() generates a fatal error, and the script will stop include() generates a warning, but the script will continue execution <html> <body> <?php require("wrongFile.php"); echo "Hello World!"; ?> </body> </html>  It is recommended to use the require() function instead of include(), because scripts should not continue after an error. By ProgrammerBlog.net http://programmerblog.net/
  • 15. PHP Built In Functions  Math functions  http://www.w3schools.com/php/php_ref_math.asp – pi() Returns the value of PI – pow() Returns the value of x to the power of y – rad2deg() Converts a radian number to a degree – rand() Returns a random integer – round() Rounds a number to the nearest integer – sin() Returns the sine of a number – sinh() Returns the hyperbolic sine of a number – sqrt() Returns the square root of a number – srand() Seeds the random number generator – tan() Returns the tangent of an angle – tanh() – abs() Returns the absolute value of a number By ProgrammerBlog.net http://programmerblog.net/
  • 16. PHP Built In Misc. Functions  Constants – M_LN10 Returns the natural logarithm of 10 (approx. 2.302) – M_PI Returns PI (approx. 3.14159) – M_SQRT2 Returns the square root of 2 (approx. 1.414)  Miscellaneous Functions – Strlen Returns length of a string – count() Returns the count of an array. – Strtolower strtolower() to lower case. – strtoupper() strtoupper() convert string to upper case By ProgrammerBlog.net http://programmerblog.net/
  • 17. PHP Fundaments - Arrays  Arrays are ordered collections of items, called elements  Each element has a value, and is identified by a key that is unique to the array It belongs to  In PHP, there are three kind of arrays: – Numeric array - An array with a numeric index – Associative array - An array where each ID key is associated with a value – Multidimensional array - An array containing one or more arrays. – $a = array(); – $state[0] = "Delaware"; – $a = array (10, 20, 30); – $a = array (’a’ => 10, ’b’ => 20, ’cee’ => 30); – $a = array (5 => 1, 3 => 2, 1 => 3,); By ProgrammerBlog.net http://programmerblog.net/
  • 18. PHP Fundaments - Arrays  Numeric Arrays: – A numeric array stores each array element with a numeric index. – $cars = array("Saab","Volvo","BMW","Toyota"); – $cars[0]="Saab"; //2nd way of declaring arrays – $cars[1]="Volvo"; – $cars[2]="BMW"; – $cars[3]="Toyota"; – echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";  Associative Arrays – An associative array, each ID key is associated with a value. – $ages = array(«John"=>32, «Jane"=>30, «David"=>34); – $ages[‘John '] = "32"; – $ages[‘Jane '] = "30"; – $ages[‘David '] = "34"; – $states = array (0 => "Alabama", "1" => "Alaska"..."49" => "Wyoming"); //numeric – $states = array ("OH" => "Ohio", "PA" => "Pennsylvania", "NY" => "New York") By ProgrammerBlog.net http://programmerblog.net/
  • 19. PHP Fundaments - Arrays  Multidimensional Arrays  Arrays of arrays, known as multidimensional arrays  In a multidimensional array, each element in the main array can also be an array.  $cars = array ( “Toyota"=>array ( “Corolla“, " Camry“, "Toyota 4Runner” ), “Suzuki"=>array ( “Vitara” ), “Honda"=>array ( "Accord“, “Sedan“, “Odyssey” ) );  echo "Is " . $ cars [Toyota '][2] . " a member of the Toyota cars?“;  Is Toyota 4Runner a member of the Toyota cars?  $states = array ( "Ohio" => array ("population" => "11,353,140", "capital" => "Columbus"), "Nebraska" => array ("population" => "1,711,263", "capital" => "Omaha") ) By ProgrammerBlog.net http://programmerblog.net/
  • 20. PHP Fundaments – Printing Arrays  print_r() and var_dump()  var_dump – var_dump() outputs the data types of each value – var_dump() is capable of outputting the value of more than one variable – var_dump($states); – $a = array (1, 2, 3); – $b = array (’a’ => 1, ’b’ => 2, ’c’ => 3); – var_dump ($a + $b); // creates union of arrays – If 2 arrays have common elements and also share same string or numeric key will appearance in result or output  print_r – print_r can return its output as a string, as opposed to writing it to the script’s standard output – print_r($states); By ProgrammerBlog.net http://programmerblog.net/
  • 21. PHP Fundaments – Comparing - Counting Arrays  $a = array (1, 2, 3); $b = array (1 => 2, 2 => 3, 0 => 1); $c = array (’a’ => 1, ’b’ => 2, ’c’ => 3); var_dump ($a == $b); // True var_dump ($a === $b); // False returns true only if the array contains the same key/value pairs in the same order var_dump ($a == $c); // True var_dump ($a === $c); // False  $a = array (1, 2, 4);  $b = array();  $c = 10;  echo count ($a); // Outputs 3  echo count ($b); // Outputs 0  echo count ($c); // Outputs 1  is_array() function : echo in_array ($a, 2); // True By ProgrammerBlog.net http://programmerblog.net/
  • 22. PHP Fundaments – Flipping and Reversing , Range  array_flip() – Exchanges all keys with their associated values in an array $trans = array("a" => 1, "b" => 1, "c" => 2); $trans = array_flip($trans); print_r($trans); – Output = Array ( [1] => b [2] => c )  array_reverse() – Return an array with elements in reverse order  array_values() – array_values — Return all the values of an array – $array = array("size" => "XL", "color" => "gold"); print_r(array_values($array)); – array_keys — Return all the keys of an array  range() – The range() function provides an easy way to quickly create and fill an array consisting of a range of low and high integer values. – $die = range(0,6); // Same as specifying $die = array(0,1,2,3,4,5,6) By ProgrammerBlog.net http://programmerblog.net/By ProgrammerBlog.net http://programmerblog.net/
  • 23. PHP Fundaments – Sorting Arrays  There are many functions in PHP core that provide various methods of sorting array contents.  sort – $array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’); sort($array); var_dump($array);  asort //to maintain key association, - $array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’); asort($array); var_dump($array);  rsort,  arsort // sorting an array in descending order By ProgrammerBlog.net http://programmerblog.net/
  • 24. PHP Fundaments – Arrays as Stack and Queues  Stack Last in First Out.  array_push() – $stack = array(); array_push($stack, ’bar’, ’baz’); var_dump($stack);  array_pop() – $states = array("Ohio","New York","California","Texas"); – $state = array_pop($states); // $state = "Texas"  Queues  array_shift, Shift an element off the beginning of array  array_unshift - Prepend one or more elements to the beginning of an array $stack = array(’qux’, ’bar’, ’baz’); $first_element = array_shift($stack); var_dump($stack); array_unshift($stack, ’foo’); var_dump($stack); By ProgrammerBlog.net http://programmerblog.net/
  • 25. Super Globals  Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.  $GLOBALS  $_SERVER  $_GET  $_POST  $_FILES  $_COOKIE  $_SESSION  $_REQUEST  $_ENV By ProgrammerBlog.net http://programmerblog.net/
  • 26. Features – Super Globals  $GLOBALS — References all variables available in global scope  <?php function test() { $foo = "local variable"; echo '$foo in global scope: ' . $GLOBALS["foo"] . "n"; echo '$foo in current scope: ' . $foo . "n"; } $foo = "Example content"; test(); ?>  $_SERVER -- $HTTP_SERVER_VARS [deprecated] — Server and execution environment information  <?php echo $_SERVER['SERVER_NAME']; ?> By ProgrammerBlog.net http://programmerblog.net/
  • 27. Super Globals  $_ENV -- $HTTP_ENV_VARS [deprecated] — Environment variables  <?php echo 'My username is ' .$_ENV["USER"] . '!'; ?>  $ip = $_SERVER['REMOTE_ADDR']; By ProgrammerBlog.net http://programmerblog.net/
  • 28. Super Globals  Thank you for viewing this slide. Hope this is helpful for you.  please visit our blog http://programmerblog.net Follow us on twitter https://twitter.com/progblogdotnet By ProgrammerBlog.net http://programmerblog.net/