Lecture Notes Of Class 10 - Strings and String Functions in PHP

Rashmi Mishra
0

Lecture Notes: Class 10 

Strings and String Functions in PHP


Objective:

1.    Learn to manipulate strings and use string functions in PHP.

2.    Practice common string operations like concatenation and replacement.

Outcome:

By the end of this class, students will be able to:

  • Understand and use common string functions such as strlen(), strpos(), substr(), and str_replace().
  • Perform string operations like concatenation, search, and replacement.

1. Introduction to Strings in PHP

A string is a sequence of characters. In PHP, strings are used to store text data and can include letters, numbers, and symbols. Strings are commonly used to process user input, manipulate text, and generate dynamic content in PHP applications.

Creating Strings

Strings in PHP can be created using:

  • Single quotes (' '): Useful when you don’t need to include variables inside the string.
  • Double quotes (" "): Allows variable interpolation (inserting variable values directly in the string).
  • Examples:

                $name = 'Alice';  // Using single quotes

                $greeting = "Hello, $name!";  // Using double quotes with variable interpolation

2. Common String Functions in PHP

PHP provides several built-in functions to manipulate strings effectively. Below are some essential string functions:

a. strlen() - String Length

The strlen() function returns the length of a string (i.e., the number of characters, including spaces).

Syntax:

strlen(string $string): int

Example:

$text = "Hello, PHP!";

$length = strlen($text);

echo "The length of the text is: " . $length; // Output: 11

b. strpos() - Find Position of a Substring

The strpos() function finds the position of the first occurrence of a substring in a string. If the substring is not found, it returns false.

Syntax:

strpos(string $haystack, string $needle, int $offset = 0): int|false

Example:

$phrase = "Learning PHP is fun!";

$position = strpos($phrase, "PHP");

if ($position !== false) {

    echo "The word 'PHP' starts at position: " . $position; // Output: 9

}

c. substr() - Extract a Substring

The substr() function extracts a portion of a string based on a starting index and an optional length.

Syntax:

substr(string $string, int $start, int $length = null): string

Example:

$text = "Hello, World!";

$substring = substr($text, 7, 5);

echo $substring; // Output: World

d. str_replace() - Replace Substring

The str_replace() function replaces occurrences of a substring within a string with another substring.

Syntax:

str_replace(mixed $search, mixed $replace, mixed $subject, int &$count = null): mixed

Example:

$text = "I love JavaScript!";

$replaced_text = str_replace("JavaScript", "PHP", $text);

echo $replaced_text; // Output: I love PHP!

3. String Concatenation in PHP

Concatenation is the process of joining two or more strings together. In PHP, concatenation is done using the dot (.) operator.

Example:

$firstName = "John";

$lastName = "Doe";

$fullName = $firstName . " " . $lastName;

echo $fullName; // Output: John Doe

4. Additional Useful String Functions

a. strtolower() and strtoupper()

  • strtolower() converts a string to lowercase.
  • strtoupper() converts a string to uppercase.

Example:

$text = "Hello World!";

echo strtolower($text); // Output: hello world!

echo strtoupper($text); // Output: HELLO WORLD!

b. ucfirst() and ucwords()

  • ucfirst() capitalizes the first letter of a string.
  • ucwords() capitalizes the first letter of each word in a string.

Example:

$text = "hello world!";

echo ucfirst($text); // Output: Hello world!

echo ucwords($text); // Output: Hello World!


5. Practical Examples and Exercises

Exercise 1: Check if a Word Exists in a Sentence

Write a PHP script to check if the word "PHP" exists in the sentence "I am learning PHP programming." If it does, output the position of the word.

$sentence = "I am learning PHP programming.";

$word = "PHP";

$position = strpos($sentence, $word);

 

if ($position !== false) {

    echo "The word 'PHP' was found at position: " . $position;

} else {

    echo "The word 'PHP' was not found.";

}

Exercise 2: Extract and Modify a Substring

Given the string "Learning PHP is easy and fun!", use substr() to extract the word "easy" and replace it with "exciting" using str_replace().

$text = "Learning PHP is easy and fun!";

$substring = substr($text, 15, 4); // Extracts "easy"

$new_text = str_replace("easy", "exciting", $text);

echo $new_text; // Output: Learning PHP is exciting and fun!


Summary

In this class, we covered:

  • Basic string functions (strlen(), strpos(), substr(), and str_replace()) for manipulating strings.
  • String concatenation to join strings using the . operator.
  • Additional functions for changing the case of strings, like strtolower(), strtoupper(), ucfirst(), and ucwords().

Next Steps

In the next class, we’ll dive deeper into handling user inputs and sanitizing strings for web applications. Practicing these string functions will build a strong foundation for text processing tasks commonly required in PHP applications.


Assignment

1.    Write a PHP script that accepts a string and returns it in uppercase, lowercase, and with the first letter of each word capitalized.

2.    Create a script that finds the length of a string, finds a particular word within it, and replaces that word with another word provided by the user.



Post a Comment

0Comments

Post a Comment (0)