27
SUBHASIS NAYAK CMC Php MySql part -2

Php, mysqlpart2

Embed Size (px)

DESCRIPTION

php mysql training(string functions)

Citation preview

Page 1: Php, mysqlpart2

SUBHASIS NAYAK

CMC

Php MySql part -2

Page 2: Php, mysqlpart2

STRING & STRING FUNCTIONSARRAYS

Content

Page 3: Php, mysqlpart2

String

A string is a collection of characters that is treated as a single entity.

In PHP strings are enclosed in quotation marks.

We can declare a string type variable by assigning it a string that is contained in Single quote Double quote

Page 4: Php, mysqlpart2

Example

$myString =“HI Good Morning”;$myString = ‘HI good Morning’;

Both are

same.

Page 5: Php, mysqlpart2

Escaping Characters

We can put double quote within single quote and single quote within double quote . as follows

$myString =“HI ‘Good Morning’ ”;$myString = ‘HI “Good Morning” ’;

Page 6: Php, mysqlpart2

Cont’d …..

However, if we want to use the same character within a quoted string, we must escape that quote by using a backslash as follows:

$myString =“HI /”Good Morning/” ”;$myString = ‘HI /’Good Morning/’ ’;

It is depend upon us

which one we want to use

double quote or Single

quote

Page 7: Php, mysqlpart2

Variables in quote

variable prefixed with a dollar sign inside a double-quoted string is replaced with its value but not in single quote.

But in a single-quoted string, the dollar sign and variable name will remain as it is.

If we want dollar sign to form part of a double-quoted string, we can also escape this by using a backslash.

$myString =“HI \$myName ”;$myString = ‘HI $myName ’;

Page 8: Php, mysqlpart2

concatenation

Strings can be joined using the period symbol as a concatenation operator.

A compound version of this operator, .=, can be used to append a string to an existing variable.

$phrase = “I want “;$phrase .= “to teach “;

Page 9: Php, mysqlpart2

Strings comparision

We can compare string values simply by using the standard comparison operators.

$strN =“Oye Oye”;if($strN == “Hulala”){echo “It’s cool”;}else{}

$strN =“Oye Oye”;if($strN == “Hulala”){echo “It’s cool”;}else{}

Page 10: Php, mysqlpart2

String Formatting

We can format the string using two powerful function. printf Sprintf

Page 11: Php, mysqlpart2

Printf

printf to display a formatted string. At its very simplest, printf takes a single string argument and behaves the same as echo.

The power of printf, however, lies in its ability to substitute values into placeholders in a string.

$price = 5.99;printf(“The price is %f”, $price);

$price = 5.99;printf(“The price is %f”, $price);

The price is 5.99The price is 5.99

Page 12: Php, mysqlpart2

Printf format cahacter

Page 13: Php, mysqlpart2

Format Codes

A format specifier can also include optional elements to specify the padding, alignment, width, and precision of the value to be displayed.

Refer some books for format codes

Refer some books for format codes

Page 14: Php, mysqlpart2

sprintf

The sprintf function is used to assign formatted strings to variables rather than displaying it.

Syntax is the same as for printf.

$price = 5.99;$str = sprintf(“The price is %f”, $price);echo $str;

$price = 5.99;$str = sprintf(“The price is %f”, $price);echo $str;

Page 15: Php, mysqlpart2

Capitalization

You can switch the capitalization of a string to all uppercase or all lower- case by using strtoupper – to upper Strtolower – to lower

$str = “I love PHP”;echo strtoupper($str) . “<br>”;echo strtolower($str) . “<br>”;

$str = “I love PHP”;echo strtoupper($str) . “<br>”;echo strtolower($str) . “<br>”;

Out put

Page 16: Php, mysqlpart2

Functions capitalize only the first character of a string is ucfirst()

$phrase = “welcome to the jungle”;echo $ucfirst($phrase);

$phrase = “welcome to the jungle”;echo $ucfirst($phrase);

Out put

Page 17: Php, mysqlpart2

Dissecting a String

substr function allows you to extract a substring by specifying a start position within the string and a length argument.

$phrase = “I love PHP”;echo substr($phrase, 3, 5);

$phrase = “I love PHP”;echo substr($phrase, 3, 5);

Out put

Page 18: Php, mysqlpart2

Position in string

To find the position of a character or a string within another string, you can use strpos.

$email = “[email protected]”;echo strpos($email, “@”);

$email = “[email protected]”;echo strpos($email, “@”);

Page 19: Php, mysqlpart2

Extract a portion from string

The function strstr extracts a portion of a string from the position at which a character or string appears up to the end of the string.

This is a convenience function that saves your using a combination of strpos and substr.

$domain = strstr($email, “@”);$domain = strstr($email, strpos($email, “@”));

$domain = strstr($email, “@”);$domain = strstr($email, strpos($email, “@”));

Out will b

e

sameOut will b

e

same

Page 20: Php, mysqlpart2

Arrays.

An array is a variable type that can store and index a set of values.

An array is useful when the data we want to store has something in common logically grouped into a set.

Page 21: Php, mysqlpart2

Creating arrays

The following PHP statement declares an array called $temps and assigns it 12 values that represent the temperatures for January through December:

To reference an indexed value from an array, you suffix the variable name with the index key.

$monthTemps = array(38, 40, 49, 60, 70, 79,

84, 83, 76, 65, 54, 42);

$monthTemps = array(38, 40, 49, 60, 70, 79,

84, 83, 76, 65, 54, 42);

To know march temp = $temp[2] ;

To know march temp = $temp[2] ;

Page 22: Php, mysqlpart2

To display content of arrays

function, print_r, that can be used to recursively output all the values stored in an array.

Page 23: Php, mysqlpart2

Looping Through an Array

We can easily replicate the way print_r loops through every element in an array by using a loop construct to perform another action for each value in the array.

By using a while loop, you can find all the index keys and their val from an array—similar to using the print_r function—as follows:

Page 24: Php, mysqlpart2

Cont’d …..

Page 25: Php, mysqlpart2

Associative Arrays

An associative array allows you to use textual keys so that the indexes can be more descriptive.

To assign a value to an array by using an associative key and to reference that value, you simply use a textual key name enclosed in quotes.

To define the complete array of average monthly temperatures

Page 26: Php, mysqlpart2

Lab question

Write a program to compare two strings and display something when test is true.

Do complex comparison in which you will check two strings with string “xyzabc” If both true display something Else display something.

Write a a program using array to find average and total of all elements.

Write a program to compare each array. If odd display odd If even display even

Write a program using associative array and display the elements.

Page 27: Php, mysqlpart2

TO TEST ALL SKILLS WE LEARNED JUST NOW.

Let’s go to lab