PHP String
Write a PHP program to reverse the string
Description:
Write a PHP program to reverse the string.
Instructions:
- You can use any string related built-in Function.
- You can use only one variable.
View Solution/Program:
<?php
$str = "Tutorials Class";
echo strrev($str);
?>
Tutorials Class - Output Window
ssalC slairotuT
Write a PHP program to find the length of the string
Description:
Write a PHP program to find the length of the string.
Instructions:
- You have to use one variable.
- You must use a built-in PHP String Function.
View Solution/Program:
<?php
$str = "Tutorials Class";
echo strlen($str);
?>
Tutorials Class - Output Window
15
Write a PHP program to count the words in the string
Description:
Write a PHP program to count the words in the string.
Instructions:
- You can use only one variable.
- You can use PHP built-in String Function.
View Solution/Program
<?php
$sample_words = "This is Tutorials Class, learn programming tutorials here.";
echo str_word_count($sample_words);
?>
Tutorials Class - Output Window
8
Write a PHP program to convert a string into uppercase
Description:
Write a PHP program to convert all the characters inside the string into uppercase.
Instructions:
- You can use only one variable.
- You should use a PHP built-in string Function
View Solution/Program
<?php
$str = "Tutorials Class";
echo strtoupper($str);
?>
Tutorials Class - Output Window
TUTORIALS CLASS
PHP Array to String Conversion (favourite colours chosen by user)
Description:
Write an exercise for PHP Array to String Conversion.
- Create a form that accept name as well as colors
- After submission, form data will be sent to another page
- Display select colors (as a list) and user name
- User name & colors selection is mandatory
View Solution/Program
<html>
<head>
<title>
array to string
</title>
</head>
<body>
<form action="data.php" method="post">
Username: <input type="text" name="username" placeholder="enter name" required/><br/><br/>
Select your favourite colors:<br/>
Red<input type="checkbox" name="check_list[]" value="red"/><br/>
Blue<input type="checkbox" name="check_list[]" value="blue"/><br/>
Green<input type="checkbox" name="check_list[]" value="green"/><br/>
Yellow<input type="checkbox" name="check_list[]" value="yellow"/><br/>
Pink<input type="checkbox" name="check_list[]" value="pink"/><br/>
Black<input type="checkbox" name="check_list[]" value="black"/><br/>
White<input type="checkbox" name="check_list[]" value="white"/><br/><br/>
<input type="submit" name="submit" value="Submit"/><br/>
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
$name = $_POST['username'];
echo $name . " 's favourite colors are " . $checked_count . " option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo "<p>" . $selected . "</p>";
}
} else {
echo "<b>Please Select Atleast One Option.</b>";
}
}