PHP find output programs (String Functions) | set 1

Find the output of PHP programs | String Functions | Set 1: Enhance the knowledge of PHP String Functions concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 24, 2021

Question 1:

<?php
    $str1 = "ABC";
    $str2 = "LMNO";
    
    $len = $str1 . strlen() + $str2 . strlen();
    echo $len;
?>

Output:

0

Explanation:

The above program will print 0 on the console screen. In the above program, we defined two-variable $str1 and $str2. Here, we used strlen() function. But the strlen() function is not used correctly.

The proper syntax for strlen() function is given below:

$len = strlen($str1) + strlen($str2);

The $str1.strlen() and $str2.strlen() will return nothing then the value of $len will be 0, that will be printed on the webpage.

Question 2:

<?php
    $str1 = "ABC";
    $str2 = "1234";
    
    $num = 5678;
    
    $len = strlen($str1) + strlen($str2) + strlen($num);
    
    echo $len;
?>

Output:

11

Explanation:

In the above program, we created three variables $str1, $str2, and $num that are initialized with "ABC", "1234", and 5678 respectively.

Here, we used strlen(), which is used to calculate the length of specified string as we as number. Let's evaluate the expression.

$len = strlen($str1) + strlen($str2)+ strlen($num);
$len = 3 + 4 + 4;
$len = 11;

Then the final value of $len will print on the web page.

Question 3:

<?php
    $str1 = "Include";
    $str2 = "Help";
    $str3 = "Com";
    
    $str = $str1 . $str2 . $str3;
    echo str_word_count($str);
?>

Output:

1

Explanation:

The above program will print 1 on the webpage. In the above program, we created three variables $str1, $str2, and $num that are initialized with "Include", "Help", and "Com" respectively.

$str =$str1.$str2.$str3;

In the above statement we concatenate $str1, $str2, and $str3, then the value of $str will be "IncludeHelpCom".

The "IncludeHelpCom" is a signal word then the str_word_count() will return 1, that will be printed on the webpage.

Question 4:

<?php
    $str1 = "Include";
    $str2 = "-Help";
    $str3 = ".Com";
    
    $str = $str1 . $str2 . $str3;
    echo str_word_count($str);
?>

Output:

2

Explanation:

In the above program, we created three variables $str1, $str2, and $num that are initialized with "Include", "-Help", and ".Com" respectively.

In the above statement we concatenate $str1, $str2, and $str3, then the value of $str will be "Include-Help.Com".

The string "Include-Help.Com" contains two words "Include-Help" and ".Com". Because words are also treat separated based on the dot '.', then str_word_count() will return 2, which will be printed on the web page.

Question 5:

<?php
    $str1 = "Include";
    $str2 = "-Help";
    $str3 = ".Com";
    
    $str = $str1 . $str2 . $str3;
    echo str_rev($str);
?>

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function str_rev() 
in /home/main.php:7
Stack trace:
#0 {main}
  thrown in /home/main.php on line 7

Explanation:

The above program will generate the compile-time error because str_rev() is not a built-in function in PHP. To reverse a string in PHP, we need to use strrev() function instead of str_rev().





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.