Web Engineering (Lecture 09) PHP part I By: Mr. Sadiq Shah Lecturer (CS) Class BS(IT)-6 th semester
PHP Data Types Variables can store data of different types, and different data types can do different things. PHP supports the following data types: String Integer Float (floating point numbers - also called double) Boolean Array Object NULL SM5312 week 5: web technology basics 2
PHP Array An array stores multiple values in one single variable. $cars = array("volvo","bmw","toyota"); PHP NULL Value $x = "Hello world!"; $x = null;
String Variables in PHP String variables are used for values that contain characters. A string can be used directly in a function or it can be stored in a variable. E.g. Echo Good morning ; OR $greetings = Good morning ; Echo $greetings;
The Concatenation Operator There is only one string operator in PHP. The concatenation operator (.) is used to put two string values together. $text1="hello "; $text2= Alice "; $text3="what a nice day!"; echo $text1. $text2. $text3;
1. The strlen() function The strlen() function is used to return the length of a string. echo strlen( google chrome ); Output: 13
2. The strpos() function The strpos() function is used to search for a character/text within a string If a match is found, this function will return the character position of the first match. If no match is found, it will return FALSE. E.g echo strpos("hello world!", w"); Output: 6
3. str_repeat() Function The str_repeat() function repeats a string a specified number of times. Syntax: str_repeat(string, repeat) Parameter string repeat Description It specifies the string to be repeated It specifies the number of times the string will be repeated. Must be greater or equal to 0 Example: echo str_repeat( Atif",10);
4. str_replace() Function The str_replace() function replaces some characters with some other characters in a string. Syntax: str_replace(find,replace,string) Parameter Description find Specifies the value to find replace Specifies the value to replace the value in find string Specifies the given string Example 1 echo str_replace( Roger","Peter","Hello Roger"); The output is: Hello Peter
5. str_word_count() Function The str_word_count() function counts the number of words in a string. Syntax: str_word_count(string) Example: echo str_word_count( Hi Good Morning ); Output: 3
6. strrev() Function The strrev() function reverses a given string. Syntax: strrev(string) Example: echo strrev( good morning ); Output: gninrom doog ih
7. strstr() function The strstr() function searches for the first occurrence of a string inside another string. This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found. Syntax: strstr(string,search) Parameter string search Description Specifies the given string to search 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 Example $search= This is BIT class ; echo strstr($search, B"); Output: BIT class
8. Strtolower() function strtolower() coverts a given string to its lower case format. Syntax: strtolower( string ) ; $lower=strtolower( Good Morning ); echo $lower; Output: good morning
9. Strtoupper() function Example strtoupper() coverts a given string to its upper case format. Syntax: strtoupper( string ) ; $upr=strtoupper( Good Morning ); echo $upr; Output: GOOD MORNING
9. Substr_replace() function substr_replace() replaces a string according a starting matching position. Syntax: substr_replace( string, replace,val1,val2); Val1 = starting point e.g. 3 Val2 = characters to be replace from starting point e.g 4 $string= goodmorning ; $result=substr_replace($string, evening,4,6); echo $result; Output: goodevening
Thank u