Practical program exercise 9 PHP String Functions for 12th computer applications
Practical program 9 PHP String Functions
String
- String is a sequence characters.
- The language comes with a full-featured library of built-in functions that let you do everything from reversing and splitting strings to calculating logarithmic values.
Using String Functions
- PHP has over 75 built-in string manipulation functions, supporting operations ranging from string repetition and reversal to comparison and search-and-replace.
Checking for Empty Strings
- The empty() function returns true if a string variable is “empty.”
- Empty string variables are those with the values ”, 0, ‘0’, or NULL.
- The empty() function also returns true when used with a non-existent variable.
strlen()
- Calculates the number of characters in a string
- The strlen() function returns the number of characters in a string.
str_word_count()
- Calculates the number of words in a string
- PHP’s str_word_count() function provides an easy way to count the number of words in a string.
strrev()
- Reverses a string
str_replace()
- Replaces parts of a string
- PHP also has the str_replace() function, designed specifically to perform find-and-replace operations.
- This function accepts three arguments: the search term, the replacement term, and the string in which to
perform the replacement.
Using Constants
- PHP containers for values that remain constant and never change.
- Constants are defined using PHP’s define() function, which accepts two arguments: the name of the constant, and its value.
- Constant names must follow the same rules as variable names, with one exception: the $ prefix is not required for constant names.
Concatenating Strings
- To combine strings, use PHP’s concatenation operator, which happens to be a period (.).
CA – 9 PHP – STRING FUNCTIONS
QUESTION:
- Write a PHP code to demonstrate the usage of string functions in PHP.
AIM:
- To create and execute String Functions in PHP.
PROCEDURE:
- Start Xampp server (Apache)
- Go to virtual path folder (C:\xampp\htdocs)
- Create test.php file and type the program
- Execute the program on your Web browser using by this URL link (http://localhost/test.php)
PROGRAM:
<html>
<body>
<?php
echo strlen(“Hello World!”).'<br>’;
echo str_word_count(“Good Morning Teachers”).”<br>”;
echo strrev(“welcome”) . “<br>”;
echo strpos(“Hello world!”,”world”).”<br>”;
echo str_replace(“Hi”,”Hello”,”Hi Everyone”).”<br>”;
define(“GREETING”,”Good Morning”);
echo GREETING.”<br>”;
$text = ‘PHP Tutorial’;
$text = preg_replace(‘/(\b[a-z])/i’,'<span style=”color:red;”>\1</span>’,$text);
echo $text;
?>
</body>
</html>
OUTPUT:
12
3
emoclew
6
Hello Everyone
Good Morning
PHP Tutorial
RESULT:
The Output was verified successfully