All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Programs Examples With Output

Last Updated : Mar 11, 2024

PHP Programs Examples With Output

In this article we will show you the solution of PHP programs examples with output, here we defined palindrome concept as we know palindrome means a word and it reverse also same result of string. We specified palindrome string on variable ‘$val’ then using loop we iterates each letters.

Within loop we are separating each letters from string by using modulus 10 and multiplying them with 10 then each time divide string by 10.

Step By Step Guide On PHP Programs Examples With Output :-

Here we defined $val variables with ‘123454321’ value then we store this value to temporary variable ‘$t’ for sum iteration result we specified variable $i with initial value ‘0’.

In while loop we specified condition ‘floor($val)’ so it will loops until it become ‘0’ and within this we separating last letter from string for each iteration by %10 then we adding that value to sum variable ‘$i*10’ at last we needs to remove last letter so we using /10.

Finally we checking both are same then displays respective result message on webpage.

<?php
$val=123454321;
$t=$val;
$i=0;
while(floor($val)){
    $m=$val%10;
    $i=$i*10+$m;
    $val=$val/10;
}
if($t==$i) echo "$i is palindrome";
else echo "$i is not palindrome";
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we specified ‘123454321’ value to variable ‘$val’ then we stored this on variable ‘$t’ for make it available as temporary and variable ‘$i’ defined for sum its initial value must be ‘0’.
  4. We defined while loop with condition ‘floor($val)’ it will iterates continuously until goes to ‘0’. Within loop we takes modulus 10 for ‘$val’ so it will returns last letter of $val stored on variable ‘$m’.
  5. We needs to multiply $i with ‘10’ then add value $m with that stored on variable ‘$i’. At last we needs to remove last letters each iteration so we dividing them by 10 then updated original $val value for next iteration.
  6. We needs to repeat this process until original value become ‘0’ so it will reach to last letter no we can stop this process finally reversed string stored on variable ‘$i’.
  7. Finally we checking original ‘$t’ with ‘$i’ both are same or not, if they same then string will be palindrome otherwise it doesn’t.

Conclusion :-

In conclusion now we are able to know how to check whether a variable is palindrome or not in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser we can see ‘123454321 is palindrome’ message otherwise it displays ‘variable value is not palindrome’.

If you’re modify variable ‘$val’ value to non palindrome value then we can see else message.

I hope this article on PHP programs examples with output helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪