Php, mysqlpart2

Post on 25-May-2015

855 views 0 download

Tags:

description

php mysql training(string functions)

Transcript of Php, mysqlpart2

SUBHASIS NAYAK

CMC

Php MySql part -2

STRING & STRING FUNCTIONSARRAYS

Content

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

Example

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

Both are

same.

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” ’;

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

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 ’;

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 “;

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{}

String Formatting

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

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

Printf format cahacter

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

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;

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

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

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

Position in string

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

$email = “chris@lightwood.net”;echo strpos($email, “@”);

$email = “chris@lightwood.net”;echo strpos($email, “@”);

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

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.

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] ;

To display content of arrays

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

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:

Cont’d …..

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

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.

TO TEST ALL SKILLS WE LEARNED JUST NOW.

Let’s go to lab