I’m a newbie in PHP and I’m confused about GET method.
Why the $text in the condition of the loop works with Appserv in Windows 7, but when I tried this code with Xampps on Mac it won’t work I’ve to use for($i=0; $i<strlen($_GET['text']); $i++) instead.
At first, I understand that after I used isset($_GET['text']) so next time I just use only $text, but now I’m confused.
<? $color = array("#FFCCFF", "#FFCCCC", "#FFCC99", "#FF99FF", "#FF99CC",
"#FF9999", "#FF66FF", "#FF66CC", "#FF6699", "#FF6666");
if (isset($_GET['text'])) {
for($i=0; $i<strlen($text); $i++) {
$j = $i%10 ?>
<font color=<?= $color[$j]?>><? echo "$text[$i]"; ?></font>
}
} else {
echo "Empty String";
} ?>
The problem is solved by many of your help.
<?php $color = array("#FFCCFF", "#FFCCCC", "#FFCC99", "#FF99FF", "#FF99CC",
"#FF9999", "#FF66FF", "#FF66CC", "#FF6699", "#FF6666");
if( isset($_GET['text'])) {
$text = $_GET['text'];
for( $i=0; $i<strlen($text); $i++) {
$j = $i%10;
echo "<font color=$color[$j]>$text[$i]</font>";
}
} else
echo "Empty string";
?>
btw I’m trying to use HTML + PHP only because I want to practice with HTML before go in deep with CSS.
The actual answer to your question, if
$textis working as an alias for$_GET['text']is probably that your Windows server is configured withregister_globalsset toon, which would mean that anything passed over in your query string would be turned into the appropriate variable.ie.
?awesome=true==$awesome = 'true'This is bad. Disable
register_globalsat the offending side, and use$_GET['text']to access your data.Your code would look better a little something like this:
Note that I have tidied up your code and made it slightly more sane/safe.
htmlentitiesis used to stop XSS vulns that could come from this, despite being unlikely due to splitting the string. You were mixing up<?php echo .. ?>and<?= .. ?>for some reason, despite them being the exact same thing. Also, don’t use<font>.You said this:
If you know you’re mixing them, why are you doing it? If you’re checking for
$_GET['text']being set, then it’s only logical that you would use that for access also.