Unit 2 122619 Web based Application Development using PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Course Details

Course Title :Web Based Application Development with PHP


Semester :CO6I
Course Code 22619
TH-ESE :70
TH-PA :30
PR-ESE :25
PR-PA :25@
SA-PA :25

1
Syllabus Details
Sr.no Chapter Name Marks
1 Expression and control Statement in 12
PHP
2 Array, Function and Graphics 16
3 Apply Object oriented concept in php 16
4 Creating and Validating Forms 12
5 Database operation 14
Total 70
2
3
Operations On String and String function

PHP String Function

4
PHP Pattern
1. str_word_count()
The str_word_count() is in-built function of PHP. It is used to return
information about words used in a string or counts the number of words in a
string.
Syntax:-str_word_count(string,return,char)
string-Specify the string to check.
return-Specify the return value of the function.
Values:
● 0 : It is by default and returns number of words found
● 1 - It returns an array with the words.
● 2 - It returns value is the actual word.
5
PHP Pattern
1. str_word_count()
<?php
$str="PHP Java";
echo "Your string is:".$str;
echo "<br>";
echo "The Total word is : " .str_word_count($str);
?>
Output:- Your String is : PHP Java
The Total Word is :2
6
PHP Pattern
2. strlen()
The strlen() function is predefined function of PHP. It is used to find the
length of string or returns the length of a string including all the whitespaces
and special character.
Syntax:-strlen(string);
<?php
Output:-Your String is :Java,
$str = 'Java';
echo "Your String is:".$str; The length of the string is :4
echo "<br>";
echo "The Length of the string is ".strlen($str);
?>

7
PHP Pattern
3. strrev()
The strrev() function is predefined function of PHP. It is used to reverse a
string. It is one of the most basic string operations which are used by
programmers and developers.
Syntax:-strrev ( string $string );
<?php
Output:-Your String is :Hello PHP,
$var1="Hello PHP";
echo "Your string is:".$var1; PHPolleH
echo "<br>";
echo strrev("$var1");
?>

8
PHP Pattern
4. strpos()
The strpos() is in-built function of PHP. It is used to find the position of the
first occurrence of a string inside another string or substring in a string.
Syntax:-mixed strpos ( string $str, mixed $find [, int $offset = 0 ] );

Parameter Description Required/Optional


string Specify the string to search. Start
find Specify the string to find. Required
start Specify where to begin the search. Optional

9
PHP Pattern
4. strpos()
<?php
$str1="I am a PHP Programmer",”am”;
echo strpos($str1)
echo "<br>";
$str2="I am a PHP Programmer",”am”,5;
echo strpos($str2);
?>
Output:-
2
16

10
PHP Pattern
5. strrpos()
The strrpos() is an in-built function of PHP which is used to find the position
of the last occurrence of a substring inside another string. It is a
case-sensitive function of PHP, which means it treats uppercase and
lowercase characters differently.
Syntax:-strrpos ($string, $search, $start)
The last occurrence of the
<?php
search string is found at
$string = "Hello! I'm Mishti Agrawal"; position: 19
$search ='gra';
$output1 = strripos( $string, $search );
echo "The last occurrence of the search string is found at position: ".$output1;
?> 11
6. strtoupper() PHP Pattern
The strtoupper() is one of the most popular functions of PHP, which is widely
used to convert the string into uppercase. It takes a string as a parameter and
converts all lowercase English character of that string to uppercase. Other
characters such as numbers and special characters of the string remain the
same.
Syntax:-strtoupper($string) All IS WELL
<?php
//original string
$input_str = "all is well";
//string converted into lowercase
$result_str = strtoupper($input_str);
echo $result_str;
?> 12
7. strtolower() PHP Pattern
The strtolower() is one of the most popular functions of PHP, which is widely
used to convert the string into lowercase. It takes a string as a parameter and
converts all uppercase English character of that string to lowercase. Other
characters such as numbers and special character of that string remain the
same.
Syntax:-strolower($string) all is well
<?php
//original string
$input_str = "All Is Well";
//string converted into lowercase
$result_str = strtolower($input_str);
echo $result_str;
?> 13
8.strcmp() PHP Pattern
String comparison is one of the most common tasks in programming and
development. strcmp() is a string comparison function in PHP. It is a built-in
function of PHP, which is case sensitive, means it treats capital and the small
case separately. It is used to compare two strings from each other. This
function compares two strings and tells whether a string is greater, less, or
equal to another string
Syntax:-strcmp($string)

14
PHP Pattern
8.strcmp()
<?php
$str1 = "hello php";
$str2 = "hello php";
echo strcoll($str1, $str2). " because both strings are equal. ";
?>

Output:-

0 Because both strings are equal.

15
PHP Pattern
9.ucwords()
The ucwords() is an in-built function of PHP, which is used to convert the first
character of each word to uppercase in a string. The ucwords() is supported by
the PHP 4 and above versions. It takes a string as an input and converts the first
character of each word of the string to uppercase. The other characters of the
string remain the same.

Syntax:-ucwords( $string, $separator) Hello, My Name Is Ravi.


<?php
$input_str = "hello, my name is ravi."; Hello|My|Name|Is| Ravi.
echo ucwords($input_str);
$input_str1= "hello, my name is ravi.";
echo ucwords($input_str1,”|”);
?> 16
PHP Pattern
10.str_replace()
The str_replace() function is a case-sensitive, built-in function of PHP which
replaces some character of the string with other characters. It is used to replace
all the occurrences of the search string with the replacement string.

Syntax:-str_replace ( $search, $replace, $string, $count)

17
PHP Pattern
10.str_replace()
<?php
$string = "Hii everyone!";
$search = 'Hii';
$replace = 'Hello';
echo '<b>'."String before replacement:".'</br></b>';
echo $string.'</br>';
$newstr = str_replace($search, $replace, $string, $count);
echo '<b>'."New replaced string is:".'</br></b>';
echo $newstr.'</br>';
echo 'Number of replacement ='.$count;
?>

18
Arrays

19
What is an Array?
▣ An array is a special variable, which can hold more than one value at a time.
▣ If you have a list of items (a list of car names, for example), storing the cars
in single variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
▣ However, what if you want to loop through the cars and find a specific one?
And what if you had not 3 cars, but 300?
▣ The solution is to create an array!
▣ An array can hold many values under a single name, and you can access the
values by referring to an index number.

20
21
Creating an Array in PHP
▣ In PHP, the array() function is used to create an array:
▣ In PHP, there are three types of arrays:
▣ Indexed arrays - Arrays with a numeric index
▣ Associative arrays - Arrays with named keys
▣ Multidimensional arrays - Arrays containing one or more arrays

22
Length of Array

❏ To Get The Length of an Array - The count() Function


❏ The count() function is used to return the length (the number of elements) of
an array:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Output - 3
23
Indexed Array

❏ There are two ways to create indexed arrays:


❏ The index can be assigned automatically (index always starts at 0), like this:
❏ $cars = array("Volvo", "BMW", "Toyota");
or
❏ the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";

24
Indexed Array
❏ The following example creates an indexed array named $cars, assigns
three elements to it, and then prints a text containing the array values:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Output-I like Volvo, BMW and Toyota.

25
Loop through Indexed Array
❏ To loop through and print all the values of an indexed array, you could use a for loop, like this:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++)
{
echo $cars[$x];
echo "<br>";
}?>
¨Output-

Volvo
BMW
Toyota
26
Associative Array

❏ Associative arrays are arrays that use named keys that you assign to them.
❏ There are two ways to create an associative array:
❏ $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

27
Associative Array
The named keys can then be used in a script:

Example

<?php

$age= array("Peter"=>"35", "Ben"=>"37", “Joe"=>"43");


echo "Peter is " . $age['Peter'] . " years old.";
?>

Output-Peter is 35 years old.

28
Loop Through an Associative Array
To loop through and print all the values of an associative array, you could use a foreach loop,
like this: Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $x_value)
{
echo "Key=" . $x . , "Value=" . $x_value;
echo "<br>";
}?>
Output
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
29
PHP Multidimensional Arrays

❏ A multidimensional array is an array containing one or more arrays.

❏ PHP supports multidimensional arrays that are two, three, four, five, or
more levels deep. However, arrays more than three levels deep are hard to
manage for most people.

30
PHP Multidimensional Arrays

❏ PHP - Two-dimensional Arrays


❏ A two-dimensional array is an array of arrays
❏ First, take a look at the following table:

Name Stock Sold


Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15

31
PHP Multidimensional Arrays
We can store the data from the table above in a two-dimensional array, like this:
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15));
Now the two-dimensional $cars array contains four arrays, and it has two indices: row
and column.
To get access to the elements of the $cars array we must point to the two indices (row
and column)

32
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15));

echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";


echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";

?>
33
<?php
$emp = array
(
array(1,"sonoo",400000),
array(2,"john",500000),
array(3,"rahul",300000)
);
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $emp[$row][$col]." ";
}
echo "<br/>";
} ?> 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy