SlideShare a Scribd company logo
Array String - Web Programming
LEARNING OUTCOMES :
Define array
Describe types of array
Modify Array Elements
Loop in Array
Define String
String Function
ARRAY
v  Array is a data structure that stores one or more similar type of values in a single
value.
v  For example if you want to store 100 numbers then instead of defining 100
variables its easy to define an array of 100 length.
v  There are three different kind of arrays and each array value is accessed using an
ID which is called array index.
ARRAY
TYPES OF ARRAY
v  Types of array are as follows :
a)  Numeric array - An array with a numeric index. Values are stored and accessed in
linear fashion
b)  Associative array - An array with strings as index. This stores element values in
association with key values rather than in a strict linear index order.
c)  Multidimensional array - An array containing one or more arrays and values are
accessed using multiple indices
NUMERIC ARRAY
v  These arrays can store numbers, strings and any object but their index will be
prepresented by numbers. By default array index starts from zero.
v  A numeric array stores each array element with a numeric index.
v  Example
Following is the example showing how to create and access numeric arrays.
Here we have used array() function to create array. This function is explained in
function reference.
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
echo "Value is $value <br />";
}
?>
<?php
/* 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 />";
}
?>
EXAMPLE
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five
OUTPUT
OR
Associative Arrays
v  The associative arrays are very similar to numeric arrays in term of functionality
but they are different in terms of their index.
v  Associative array will have their index as string so that you can establish a strong
association between key and values.
v  To store the salaries of employees in an array, a numerically indexed array would
not be the best choice. Instead, we could use the employees names as the keys
in our associative array, and the value would be their respective salary.
v  ID key is associated with a value.
v  Storing data about specific named values
NOTE: Don't keep associative array inside double quote while printing otheriwse it
would not return any value.
<?php
/* First method to associate create array. */
$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
?>
EXAMPLE
<?php
/* Second method to create array. */
$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
?>
EXAMPLE
ID KEY
Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low
OUTPUT
Multidimensional Arrays
v  A multi-dimensional array each element in the main array can also be an array.
And each element in the sub-array can be an array, and so on. Values in the multi-
dimensional array are accessed using multiple index.
v  Array of array
v  Example
In this example we create a two dimensional array to store marks of three
students in three subjects:
This example is an associative array, you can create numeric array in the same
fashion.
<?php
$marks = array("mohammad" => array("physics" => 35, "maths" => 30, "chemistry" => 39),
"qadir" => array("physics" => 30,"maths" => 32, "chemistry”=> 29),
"zara" => array("physics" => 31,"maths" => 22,"chemistry" => 39));
/* Accessing multi-dimensional array values */
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for qadir in maths : ";
echo $marks['qadir']['maths'] . "<br />";
echo "Marks for zara in chemistry : " ;
echo $marks['zara']['chemistry'] . "<br />";
?>
Example 1
OUTPUT
Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39
Example 2 : AUTOMATIC ID KEY
OUTPUT
Example 2 : SPESIFIC ID KEY
OUTPUT
MODIFY ARRAY ELEMENTS
v  Array functions allow to manipulate arrays. PHP function (built in) for array :
a)  array() – create array
b)  array_count_values()
c)  array_combine()
d)  array_intersect()
e)  array_diff()
f)  array_sum()
g)  in_array()
www.w3schools.com/php/php_ref_array.asp
array_count_values()
array_combine()
array_intersect()
array_diff()
array_sum()
In_array()
Loop function in array
v  Foreach Loop : Example 1 (Automatic)
Loop function in array
v  Foreach Loop : Example 2 (Specific ID)
Loop function in array
v  Foreach Loop : Example 2 (Specific ID)
EXERCISE
QUESTION
<?php
$cars=array("Volvo”,BMW","Toyota", “Audi”);
$arrlength=count($cars);
for($x=0;$x<$arrlength;$x++)
  {
  echo $cars[$x];
   }
?>
EXERCISE
OUTPUT 1
Volvo, BMW, Toyota, Audi
STRING
v The string functions allow you to manipulate
strings.
v A string variable is used to store and manipulate
text.
v String variables are used for values that contain
characters.
STRLEN() FUNCTION
The strlen() function returns the length of a string.
DEFINITION & USAGE
strlen(string)
SYNTAX
STRLEN() FUNCTION
Parameter Description
string Required. Specifies the string to
check
strlen() function
12
OUTPUT
<?php
echo strlen("Hello world!");
?>
EXAMPLE
strpos() function
q The strpos() function finds the position of the last occurrence of a string
inside another string.
q This function returns the position on success, otherwise it returns
FALSE.
DEFINITION & USAGE
strpos(string,find,start)
SYNTAX
strpos() function
Parameter Description
string Required. Specifies the string to search
find Required. Specifies the string to find
start Optional. Specifies where to begin the search
strpos() function
<?php
echo strpos("Hello world!","world");
?>
EXAMPLE
6
OUTPUT
strpos() function
NOTE
The position of the string "world" in the example
above is 6. The reason that it is 6 (and not 7), is
that the first character position in the string is 0,
and not 1.
strstr() function
q  The strstr() function searches for the first occurrence of a string
inside another string.
q  This function returns the rest of the string (from the matching point),
or FALSE, if the string to search for is not found.
DEFINITION & USAGE
strstr(string,search)
SYNTAX
strstr() function
Parameter Description
string Required. Specifies the string to search
search Required. Specifies the string to search for. If this
parameter is a number, it will search for the
character matching the ASCII value of the number
strstr() function
<?php
echo strstr("Hello world!","world");
?>
EXAMPLE 1
world!
OUTPUT
substr() function
The substr() function returns a part of a
string.
DEFINITION & USAGE
substr(string,start,optional_length)
SYNTAX
substr() function
Parameter Description
string Required. Specifies the string to return a part of
start • Required. Specifies where to start in the string. A positive number - Start at
a
specified position in the string
• A negative number - Start at a specified position from the end of the string
• 0 - Start at the first character in string
length • Optional. Specifies the length of the returned string. Default is to the end of
the string. A positive number - The length to be returned from the start
parameter
• Negative number - The length to be returned from the end of the string
substr() function
<?php
echo substr("Hello world!",6);
?>
EXAMPLE 1
world!
OUTPUT
substr() function
<?php
echo substr("Hello world!",6,5);
?>
EXAMPLE 2
world
OUTPUT
EXERCISE
<?php
echo strpos("My Unexpected
String!","ex");
?>
QUESTION 1
EXERCISE
OUTPUT 1
5
EXERCISE
<?php
echo substr('abcdefghijk', 1);
?>
QUESTION 2
EXERCISE
bcdefghijk
OUTPUT 2
EXERCISE
QUESTION 3
<?php
echo strlen(“welcome back!");
?>
EXERCISE
OUTPUT 3
13
EXERCISE
QUESTION 4
<?php
echo strstr(“welcome back",“wel");
?>
EXERCISE
OUTPUT 4
welcome back
SUMMARY

More Related Content

PPTX
PHP array 1
Mudasir Syed
 
PPTX
Chap 3php array part1
monikadeshmane
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PDF
4.1 PHP Arrays
Jalpesh Vasa
 
PDF
PHP Unit 4 arrays
Kumar
 
PDF
Php array
Nikul Shah
 
PHP array 1
Mudasir Syed
 
Chap 3php array part1
monikadeshmane
 
Arrays in PHP
Vineet Kumar Saini
 
4.1 PHP Arrays
Jalpesh Vasa
 
PHP Unit 4 arrays
Kumar
 
Php array
Nikul Shah
 

What's hot (19)

PPTX
String variable in php
chantholnet
 
PDF
Introduction to ad-3.4, an automatic differentiation library in Haskell
nebuta
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PPT
Class 5 - PHP Strings
Ahmed Swilam
 
PDF
Introduction to ad-3.4, an automatic differentiation library in Haskell
nebuta
 
PPT
Php Using Arrays
mussawir20
 
PDF
Implode & Explode in PHP
Vineet Kumar Saini
 
PPT
Php array
Core Lee
 
PPT
Csharp4 arrays and_tuples
Abed Bukhari
 
PPT
PHP array 2
Mudasir Syed
 
PDF
Scripting3
Nao Dara
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PDF
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
PDF
Chapter 13.1.7
patcha535
 
PPTX
Array in php
ilakkiya
 
PPT
Php basics
hamfu
 
PDF
Array&amp;string
chanchal ghosh
 
String variable in php
chantholnet
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
nebuta
 
Class 4 - PHP Arrays
Ahmed Swilam
 
Class 5 - PHP Strings
Ahmed Swilam
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
nebuta
 
Php Using Arrays
mussawir20
 
Implode & Explode in PHP
Vineet Kumar Saini
 
Php array
Core Lee
 
Csharp4 arrays and_tuples
Abed Bukhari
 
PHP array 2
Mudasir Syed
 
Scripting3
Nao Dara
 
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Chapter 13.1.7
patcha535
 
Array in php
ilakkiya
 
Php basics
hamfu
 
Array&amp;string
chanchal ghosh
 
Ad

Similar to Array String - Web Programming (20)

PPTX
Unit 2-Arrays.pptx
mythili213835
 
PPTX
Chapter 2 wbp.pptx
40NehaPagariya
 
DOCX
Array andfunction
Girmachew Tilahun
 
PPT
Web Technology - PHP Arrays
Tarang Desai
 
PPTX
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
PPTX
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
PPTX
PHP Arrays_Introduction
To Sum It Up
 
PPSX
DIWE - Advanced PHP Concepts
Rasan Samarasinghe
 
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPT
PHP and MySQL with snapshots
richambra
 
PPT
Php, mysqlpart2
Subhasis Nayak
 
PPTX
Arrays in PHP
davidahaskins
 
PPTX
Array
hinanshu
 
PPTX
Arrays in php
soumyaharitha
 
PPT
Arrays in php
Laiby Thomas
 
PPT
PHP-04-Arrays.ppt
Leandro660423
 
PPTX
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
PPT
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
Unit 2-Arrays.pptx
mythili213835
 
Chapter 2 wbp.pptx
40NehaPagariya
 
Array andfunction
Girmachew Tilahun
 
Web Technology - PHP Arrays
Tarang Desai
 
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
PHP Arrays_Introduction
To Sum It Up
 
DIWE - Advanced PHP Concepts
Rasan Samarasinghe
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PHP Functions & Arrays
Henry Osborne
 
PHP and MySQL with snapshots
richambra
 
Php, mysqlpart2
Subhasis Nayak
 
Arrays in PHP
davidahaskins
 
Array
hinanshu
 
Arrays in php
soumyaharitha
 
Arrays in php
Laiby Thomas
 
PHP-04-Arrays.ppt
Leandro660423
 
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
Ad

Recently uploaded (20)

PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
oapresentation.pptx
mehatdhavalrajubhai
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 

Array String - Web Programming

  • 2. LEARNING OUTCOMES : Define array Describe types of array Modify Array Elements Loop in Array Define String String Function
  • 3. ARRAY v  Array is a data structure that stores one or more similar type of values in a single value. v  For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length. v  There are three different kind of arrays and each array value is accessed using an ID which is called array index.
  • 5. TYPES OF ARRAY v  Types of array are as follows : a)  Numeric array - An array with a numeric index. Values are stored and accessed in linear fashion b)  Associative array - An array with strings as index. This stores element values in association with key values rather than in a strict linear index order. c)  Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices
  • 6. NUMERIC ARRAY v  These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero. v  A numeric array stores each array element with a numeric index. v  Example Following is the example showing how to create and access numeric arrays. Here we have used array() function to create array. This function is explained in function reference.
  • 7. <?php /* First method to create array. */ $numbers = array( 1, 2, 3, 4, 5); foreach( $numbers as $value ) { echo "Value is $value <br />"; } ?> <?php /* 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 />"; } ?> EXAMPLE
  • 8. Value is 1 Value is 2 Value is 3 Value is 4 Value is 5 Value is one Value is two Value is three Value is four Value is five OUTPUT OR
  • 9. Associative Arrays v  The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. v  Associative array will have their index as string so that you can establish a strong association between key and values. v  To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary. v  ID key is associated with a value. v  Storing data about specific named values NOTE: Don't keep associative array inside double quote while printing otheriwse it would not return any value.
  • 10. <?php /* First method to associate create array. */ $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500); echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />"; echo "Salary of qadir is ". $salaries['qadir']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; ?> EXAMPLE
  • 11. <?php /* Second method to create array. */ $salaries['mohammad'] = "high"; $salaries['qadir'] = "medium"; $salaries['zara'] = "low"; echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />"; echo "Salary of qadir is ". $salaries['qadir']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; ?> EXAMPLE ID KEY
  • 12. Salary of mohammad is 2000 Salary of qadir is 1000 Salary of zara is 500 Salary of mohammad is high Salary of qadir is medium Salary of zara is low OUTPUT
  • 13. Multidimensional Arrays v  A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi- dimensional array are accessed using multiple index. v  Array of array v  Example In this example we create a two dimensional array to store marks of three students in three subjects: This example is an associative array, you can create numeric array in the same fashion.
  • 14. <?php $marks = array("mohammad" => array("physics" => 35, "maths" => 30, "chemistry" => 39), "qadir" => array("physics" => 30,"maths" => 32, "chemistry”=> 29), "zara" => array("physics" => 31,"maths" => 22,"chemistry" => 39)); /* Accessing multi-dimensional array values */ echo "Marks for mohammad in physics : " ; echo $marks['mohammad']['physics'] . "<br />"; echo "Marks for qadir in maths : "; echo $marks['qadir']['maths'] . "<br />"; echo "Marks for zara in chemistry : " ; echo $marks['zara']['chemistry'] . "<br />"; ?> Example 1
  • 15. OUTPUT Marks for mohammad in physics : 35 Marks for qadir in maths : 32 Marks for zara in chemistry : 39
  • 16. Example 2 : AUTOMATIC ID KEY
  • 18. Example 2 : SPESIFIC ID KEY
  • 20. MODIFY ARRAY ELEMENTS v  Array functions allow to manipulate arrays. PHP function (built in) for array : a)  array() – create array b)  array_count_values() c)  array_combine() d)  array_intersect() e)  array_diff() f)  array_sum() g)  in_array() www.w3schools.com/php/php_ref_array.asp
  • 27. Loop function in array v  Foreach Loop : Example 1 (Automatic)
  • 28. Loop function in array v  Foreach Loop : Example 2 (Specific ID)
  • 29. Loop function in array v  Foreach Loop : Example 2 (Specific ID)
  • 32. STRING v The string functions allow you to manipulate strings. v A string variable is used to store and manipulate text. v String variables are used for values that contain characters.
  • 33. STRLEN() FUNCTION The strlen() function returns the length of a string. DEFINITION & USAGE strlen(string) SYNTAX
  • 34. STRLEN() FUNCTION Parameter Description string Required. Specifies the string to check
  • 36. strpos() function q The strpos() function finds the position of the last occurrence of a string inside another string. q This function returns the position on success, otherwise it returns FALSE. DEFINITION & USAGE strpos(string,find,start) SYNTAX
  • 37. strpos() function Parameter Description string Required. Specifies the string to search find Required. Specifies the string to find start Optional. Specifies where to begin the search
  • 38. strpos() function <?php echo strpos("Hello world!","world"); ?> EXAMPLE 6 OUTPUT
  • 39. strpos() function NOTE The position of the string "world" in the example above is 6. The reason that it is 6 (and not 7), is that the first character position in the string is 0, and not 1.
  • 40. strstr() function q  The strstr() function searches for the first occurrence of a string inside another string. q  This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found. DEFINITION & USAGE strstr(string,search) SYNTAX
  • 41. strstr() function Parameter Description string Required. Specifies the string to search search Required. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number
  • 42. strstr() function <?php echo strstr("Hello world!","world"); ?> EXAMPLE 1 world! OUTPUT
  • 43. substr() function The substr() function returns a part of a string. DEFINITION & USAGE substr(string,start,optional_length) SYNTAX
  • 44. substr() function Parameter Description string Required. Specifies the string to return a part of start • Required. Specifies where to start in the string. A positive number - Start at a specified position in the string • A negative number - Start at a specified position from the end of the string • 0 - Start at the first character in string length • Optional. Specifies the length of the returned string. Default is to the end of the string. A positive number - The length to be returned from the start parameter • Negative number - The length to be returned from the end of the string
  • 45. substr() function <?php echo substr("Hello world!",6); ?> EXAMPLE 1 world! OUTPUT
  • 46. substr() function <?php echo substr("Hello world!",6,5); ?> EXAMPLE 2 world OUTPUT