Once again we are going to write the code to get our job done without using any single function. We will have an input from the user and then we will tell him what word you have entered the longest. Little bit logical code that you have to understand deeply. Let’s do it.
Code Example:
<form method="post"> <input type="text" name="txt" /><br /> <input type="submit" name="sbt" /> </form> <?php error_reporting(0); $sentence = $_POST['txt']; $z = 0; $x = 0; $wArray = array(); while( isset($sentence[$z]) ){ if( $sentence[$z] != " " ){ $wArray[$x] .= $sentence[$z]; } else{ $x++; } $z++; } $longest = 0; $lonWord = ''; foreach ($wArray as $word) { $p = 0; while( isset($word[$p]) ){ $p++; } if ($p > $longest) { $longest = $p; $lonWord = $word; } } echo "<b>You have entered : </b>".$sentence; echo "<br />"; echo "<b>Longest Word is : </b>".$lonWord; ?>
Result :
You have entered : get longest word without using function in php
Longest Word is : function