The for
keyword indicates a loop in PHP. The for
loop executes a block of statements repeatedly until the specified condition returns false.
You may also like: PHP for loop examples
Code: Write a php program that will count the characters on the string specified by the user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Code4Example</title> </head> <body> <?php $text="code4examples - php examples - php for loop examples"; $search_char="e"; $count="0"; for($x="0"; $x< strlen($text); $x++) { if(substr($text,$x,1)==$search_char) { $count=$count+1; } } echo $count."<br>"; ?> </body> </html> |