ADVERTISEMENT
ADVERTISEMENT

PHP find output programs (Number Functions) | set 1

Find the output of PHP programs | Number 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
    $num1 = "123";
    $num2 = 123;
    
    if (is_int($num1) == true) echo "num1 is an integer number";
    else echo "num1 is not an integer number";
    
    echo '<br>';
    
    if (is_int($num2) == true) echo "num2 is an integer number";
    else echo "num2 is not an integer number";
?>

Output:

num1 is not an integer number
num2 is an integer number

Explanation:

In the above program, we created two variables $num1 and $num2 initialized with "123" and 123 respectively. Here, we used the is_int() function, which is used to check the specified variable contains an integer number or not.

In the above program, $num1 is a string, while $num2 contains an integer number. Then firs if-else block will print "num1 is not an integer number". And the second if-else block will print "num2 is an integer number" on the web page.

Question 2:

<?php
    $num = 12.24;
    
    if (is_float($num) == true) echo "num is a float number";
    else echo "num is not a float number";
    
    echo '<br>';
    
    if (is_double($num) == true) echo "num is a double number";
    else echo "num is not a double number";
?>

Output:

num is a float number
num is a double number

Explanation:

In the above program, we declared a variable $num initialized with 12.24. Here, we used two built-in functions is_float() and is_double(). is_double and is_float() have functionality. is_double() is the alias of is_float(). Then both functions will return true, and then it will print "num is a float number" and after the newline, "num is a double number" will be printed on the webpage.

Question 3:

<?php
    $num1 = "12.24";
    $num2 = "1224";
    $num3 = "A123";
    
    if (is_numeric($num1) == true) echo "num1 is numeric";
    else echo "num1 is not numeric";
    
    echo '<br>';
    
    if (is_numeric($num2) == true) echo "num2 is numeric";
    else echo "num2 is not numeric";
    
    echo '<br>';
    
    if (is_numeric($num3) == true) echo "num3 is numeric";
    else echo "num3 is not numeric";
?>

Output:

num1 is numeric
num2 is numeric
num3 is not numeric

Explanation:

In the above program, we created three variables $num1, $num2, and $num3 initialized with "12.24", "1224" and "A123" respectively. Here, we used the is_numeric() function, which is used to check the specified variable contains a numeric value or not.

In the above program, $num1 is a floating-point string, while $num2 contains an integer string, and $num3 contains an alphanumeric string. Then firs if-else block will print "num1 is numeric". And the second if-else block will print "num2 is numeric" and third if-else block will print "num3 is not numeric" on the web page.

Question 4:

<?php
    $num1 = 12.24;
    
    $num2 = (int)$num1;
    $num3 = int($num1);
    
    echo $num2 . '\n';
    echo $num3 . '\n';
?>

Output:

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

Explanation:

The above program will generate the compile-time error, int() is not a built-in function in PHP. While the below statement is used to typecast the number $num1 from floating-point number to integer number,

$num2 = (int)$num1;

Question 5:

<?php
    $num1 = 12.24;
    
    $num2 = (int)$num1;
    $num3 = intval($num1);
    
    echo $num2 . '\n';
    echo $num3 . '\n';
?>

Output:

12\n12\n

Explanation:

In the above program, we created a variable $num1, which is initialized with 12.24. The below statement is used to typecast the number $num1 from floating-point number to integer number.

$num2 = (int)$num1;

After that, we used int_val(), which is used to return an integer value, and then we used '\n' instead of '<br>' tag for a new line.

So finally, "12\n12\n" will be printed on the webpage.

Question 6:

<?php
    $num1 = "1234";
    $num2 = intval($num1) + 6;
    
    echo $num2;
?>

Output:

1240

Explanation:

The above program will print 1240 on the webpage because the intval() function returns the number corresponding to the specified numeric string.

$num2 = intval($num1)+6;

In the above statement intval($num1) returns integer number 1234 and then we added 6 to it, then 1240 assigned to $num2.

Then the final value of $num2 is 1240, will be printed on the webpage.

Question 7:

<?php
    $num1 = "12.34";
    $num2 = (integer)$num1 + 6;
    
    echo $num2;
?>

Output:

18

Explanation:

In the program, we created a variable $num1, which is initialized with a floating-point string.

$num2 = (integer)$num1+6;

In the above statement, we typecast $num1 into integer and added 6 to it, then 18 assigned to $num2.

Then the final value of $num2 is 18, will be printed on the webpage.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.