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

What's hot (19)

String variable in php
String variable in phpString variable in php
String variable in php
chantholnet
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
nebuta
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
nebuta
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
Vineet Kumar Saini
 
Php array
Php arrayPhp array
Php array
Core Lee
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
Abed Bukhari
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
Mudasir Syed
 
Intoduction to php arrays
Intoduction to php arraysIntoduction to php arrays
Intoduction to php arrays
baabtra.com - No. 1 supplier of quality freshers
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
Compare Infobase Limited
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Chapter 13.1.7
Chapter 13.1.7Chapter 13.1.7
Chapter 13.1.7
patcha535
 
Array in php
Array in phpArray in php
Array in php
ilakkiya
 
Php basics
Php basicsPhp basics
Php basics
hamfu
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
chanchal ghosh
 
Array
ArrayArray
Array
Kathmandu University
 

Similar to Array String - Web Programming (20)

Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
mythili213835
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
40NehaPagariya
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
Girmachew Tilahun
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP Arrays
Tarang Desai
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Arrays syntax and it's functions in php.pptx
Arrays syntax and it's  functions in php.pptxArrays syntax and it's  functions in php.pptx
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
PHP Arrays_Introduction
PHP Arrays_IntroductionPHP Arrays_Introduction
PHP Arrays_Introduction
To Sum It Up
 
DIWE - Advanced PHP Concepts
DIWE - Advanced PHP ConceptsDIWE - Advanced PHP Concepts
DIWE - Advanced PHP Concepts
Rasan Samarasinghe
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
richambra
 
Php, mysqlpart2
Php, mysqlpart2Php, mysqlpart2
Php, mysqlpart2
Subhasis Nayak
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
davidahaskins
 
Array
ArrayArray
Array
hinanshu
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
soumyaharitha
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
Laiby Thomas
 
PHP-04-Arrays.ppt
PHP-04-Arrays.pptPHP-04-Arrays.ppt
PHP-04-Arrays.ppt
Leandro660423
 
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
 
PHP - Introduction to PHP Arrays
PHP -  Introduction to PHP ArraysPHP -  Introduction to PHP Arrays
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP Arrays
Tarang Desai
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Arrays syntax and it's functions in php.pptx
Arrays syntax and it's  functions in php.pptxArrays syntax and it's  functions in php.pptx
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
PHP Arrays_Introduction
PHP Arrays_IntroductionPHP Arrays_Introduction
PHP Arrays_Introduction
To Sum It Up
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.netPhp my sql - functions - arrays - tutorial - programmerblog.net
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
richambra
 
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
 
Ad

Recently uploaded (20)

Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free DownloadWondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
BradBedford3
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
Leveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer IntentsLeveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer Intents
Keheliya Gallaba
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
Insurance Tech Services
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free DownloadWondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
BradBedford3
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
Leveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer IntentsLeveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer Intents
Keheliya Gallaba
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
Insurance Tech Services
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Ad

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