PHP Arrays Functions.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
ITC 240: Web Application Programming
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Outline o What is an array ? o Indexed array o Associative array o Multidimensional array.
Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
PHP Constructs Advance Database Management Systems Lab no.3.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server.
Creating PHP Pages Chapter 10 PHP Arrays. Arrays An array can store one or more values in a single variable name. An element of an associative accessed.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
PHP using MySQL Database for Web Development (part II)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
CGS 3066: Web Programming and Design Spring 2017
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Session 2 Basics of PHP.
>> Fundamental Concepts in PHP
Review 3 csc242 – web programming.
Test 2 Review Outline.
Functions A function is a block of code with a name. function functionName() { code to be executed; }
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Arrays: Checkboxes and Textareas
DBW - PHP DBW2017.
Arrays in PHP are quite versatile
Arrays An array in PHP is an ordered map
Arrays and Multidimensional Arrays
CHAPTER 5 SERVER SIDE SCRIPTING
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
Web Technologies PHP 5 Basic Language.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
[Array, Array, Array, Array]
8th Semester, Batch 2008 Department of Computer Science SSUET.
Chapter 5 Structures.
While Loops BIS1523 – Lecture 12.
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Arrays, For loop While loop Do while loop
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
Functions BIS1523 – Lecture 17.
Web Systems Development (CSC-215)
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
PHP: Basics FdSc Module 109 Server side scripting and Database design
LabVIEW.
[Array, Array, Array, Array]
Kirkwood Center for Continuing Education
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Multidimensional array
Basics (Cont...).
Conditional statement & LOOPS
Web Programming Language
ITM 352 Simple Arrays.
Web Programming– UFCFB Lecture 21
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Fundamental Programming
PHP an introduction.
Loops and Conditionals Generating , Reading files.
PHP PROF. S. LAKSHMANAN, DEPT. OF B. VOC. (SD & SA),
LOOPING STRUCTURE Chapter - 7 Padasalai
PHP Array.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

PHP Arrays Functions

Array An array can store many values at once within a single variable. Types of Arrays in PHP There are three types of arrays that you can create. These are: Indexed array — An array with a numeric key. Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.

Indexed Arrays An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is <?php $colors = array("Red", "Green", "Blue"); print_r($colors); ?>

Print_r() function The print_r() function is used to print human-readable information about a variable.  the variable is an integer or a float or a string the function returns value of the variable. If the variable is an array the function returns keys and elements.

Indexed Arrays-Example <?php $cname= array ('Pakistan', 'Saudia Arabia','Turkey'); $salary= array (400, 3800,6700); print_r($cname); echo '<br>'; print_r($salary); echo '<hr>'; ?>

How to to extract through loops <?php $colors = array("Red", "Green", "Blue"); for ($a=0; $a<3; $a++) { echo "Name:". $colors[$a]. "<br>"; } echo "<hr>";

Foreach Loop syntax The “foreach” loop gives PHP an easy way to iterate over arrays and can only be used on arrays. First syntax foreach ($array as $value) { code to be executed; } Second syntax foreach ($array as $key => $value) { code to be executed;

For Loop-indexed array <?php $colors = array("Red", "Green", "Blue"); for ($a=0; $a<3; $a++) { echo "Name:". $colors[$a]. "<br>"; } Output:

Fore each-indexed array $books= array ("PHP","C++","Java","Perl”,"python"); foreach ( $books as $val) { echo $val. "<br>"; Output: }

How to use counter to display data $counter=1; foreach ( $books as $val) { echo $counter . ". " . $val. "<br>"; $counter++; } Output: echo "<br>";

Associative Arrays $ages = array("Nauman"=>22, "Hamid"=>32, "Danish"=>28); foreach ($ages as $x=>$y) { echo "Name: ". $x. "<br>"; echo "Age:" . $y. "<br>"; Output: }

Associative array: display in HTML Table $ages = array("Nauman"=>22, "Hamid"=>32, "Danish"=>28); echo "<table border=1> <tr><td> Name</td> <td> Age</td> </tr>"; foreach ($ages as $x=>$y) { echo "<tr><td>" . $x . "</td>"; echo "<td>" . $y . "</td></tr>"; } echo "</table>"; Output:

Multidimensional array $contacts = array( array( "name" => "Nauman", "email" => "nauman@mail.com", ), "name" => "Hamid", "email" => "hamid@mail.com", "name" => "Danish", "email" => "danish@mail.com", ) );

Multidimension array-Example $contacts = array( array( "name" => "Nauman", "email" => "nauman@mail.com", ), "name" => "Hamid", "email" => "hamid@mail.com", "name" => "Danish", "email" => "danish@mail.com", ) );

Multidimensional array..con foreach ($contacts as $contacts_1) foreach ($contacts_1 as $x=>$y ) { echo $x . ":" . $y ."<br>"; } Output: