Php Alphabet Loop
There are two problems in your code.
At first, single quotes ( ' ) behave differently than a string with two quotes ( " ). When using strings with single quotes, escape sequences (except \' and \\ ) are not interpreted, and variables are not consumed. This can be fixed as such ( removing quotes or changing them to double quotes):
$string = 'hey'; foreach(range('a','z') as $i) { if($string == $i) { echo $i; } } Secondly, your condition will never be evaluated as TRUE , since 'hey' will never be equal to one letter of the alphabet. To evaluate if a letter is in a word, you can use strpos() :
$string = 'hey'; foreach(range('a','z') as $i) { if(strpos($string, $i) !== FALSE) { echo $i; } } !== FALSE important in this case, since 0 also evaluates to FALSE . This means that if you delete !== FALSE , your first character will not be output.
PHP documentation:
strpos()
PHP Documentation: Boolean Conversion
PHP Documentation: Comparison Operators