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 $text is working as an alias for $_GET['text'] is probably that your Windows server is configured with register_globals set to on, 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_globals at the offending side, and use $_GET['text'] to access your data.
Your code would look better a little something like this:
<?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; ?>
<span style="color: <?= $color[$j] ?>"><?= htmlentities($text[$i]); ?></span>
<?php }
} else {
echo "Empty String";
}
?>
Note that I have tidied up your code and made it slightly more sane/safe. htmlentities is 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:
At first, I understand that "after I used isset($_GET['text']) so next time I just use only($text), but now I'm confused.
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.
Okay, why are you dropping in and out of PHP every line? It is allowed to have more than one line of PHP at a time, you know!
$_GET['text'] is a variable. Accessing it does nothing special, but it is special in that you can access it regardless of scope (it is superglobal). Referring to is as $text only works if the autoregister globals setting is enabled, which is not recommended for various reasons.
So, your code should look like:
<?php
$color = array(".....");
if( isset($_GET['text'])) {
$l = strlen($_GET['text']);
for( $i=0; $i<$l; $i++) {
$j = $i%10;
echo "<span style=\"color: ".$color[$j].";\">".$text[$i]."</span>";
}
}
else echo "Empty string";
?>
I also took the liberty of updating your HTML out of the last milennium.
You should initialize $text variable first, something like this:
$text = $_GET['text'];
This should work without any problems.
I'm still unsure as to what you're doing, but:
$colours = 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 "<span style='color: {$colours[$j]}'>{$text[$i]}</span>";
}
}
else {
echo 'No text';
}
Related
I have a simple code that goes like this
<?php
echo $passer;
for($i=1; $i<=5; $i++){
$msg = $i;
}
$passer = $msg;
?>
My aim is to display the result above the loop. Is there a way to pass the value of $msg so it will be displayed above the loop? Currently the output is:
Undefined variable: passer
First you are getting error Undefined variable: passer. because you are using variable without defining. So define variable before using:
$passer = 0; //defining variable
echo $passer;// you are getting error here
for($i=1; $i<=5; $i++){
$msg = $i;
}
$passer = $msg;
Above code will not give you output as you expecting. because you are echoing $passer before initializing it. Try something like this:
for($i=1; $i<=5; $i++){
$msg = $i;
}
$passer = $msg;//initializing first
echo $passer;//output 5
Still in above solution i am echoing $passer after processing of for loop.
Because parser follows top to bottom approach so its impossible output
above loop.
$msg = array();
for($i=1; $i<=5; $i++){
$msg[] = $i;
}
$passer = $msg;
print_r($passer);
No One Language can output value before define that.
but you can like this to get your result.
<?php ob_start() ?>
##passer##
<?php
for($i=1; $i<=5; $i++){
$msg = $i;
} ?>
<?php echo str_replace("##passer##", $msg, ob_get_clean()) ?>
Excuse me if the title isn't completely clear.
but i've got a value that is the outcome of a sum. Called $addCols and a variable that is just html. What i want is to repeat the html with the addCols variable.
$addCols = 4 //for example
$html .= '<div>test</div>';
And i wish to get the following result:
// Result
test
test
test
test
What i've tried:
$result = $addCols * $html;
echo $result;
There is a built-in function for this:
echo str_repeat("<div>test</div>", $addCols);
Documentation
Built-in functions will always be better/faster than manually-coded solutions.
create a for loop, this will post the html code as many times as your variable states giving the requested output.
for($x = 0; $x < $addCols; $x++)
{
echo '<div>text</div>';
}
Just use str_repeat() function in php or run a loop. Use the code below
With str_repeat
$addCols = 4 ;//for example
$html = '<div>test</div>';
echo str_repeat($html, $addCols);
With loop
$addCols = 4; //for example
$html = '<div>test</div>';
for($x = 0; $x < $addCols; $x++)
{
echo $html;
}
With while loop
$html = '<div>test</div>';
$addCols = 4 ;//for example
$x=0;
while($x < $addCols)
{
echo $html;
$x++;
}
Hope this helps you
I have a file called "data.txt". In this file, there are 30 lines with strings who all have the same length. It looks like this:
2QA4ZRDUT
IDVLTLZSC
4GYC3HCMV
1W6409JD5
70P7U66TE
... and so on.
What I want to do now is reformatting these lines. I want 5 strings on one line, seperated with a ";". After that, I want a new line and the next 5. In the end, it should look like this:
2QA4ZRDUT;IDVLTLZSC;4GYC3HCMV;1W6409JD5;70P7U66TE;
NGN1TGF6G;JWVI7LSIZ;U99TMVXXK;KLBDMRPQV;MEFKLUO3;
... and so on, until the whole content of the file is reformatted like this.
The last hours I've been trying to accomplish this by using for, foreach and while loops but I only manage to get one line. I hope somebody can help me since I'm not that experienced with PHP.
This is the content of "data.txt":
2QA4ZRDUT
IDVLTLZSC
4GYC3HCMV
1W6409JD5
70P7U66TE
OG2JBBZF6
5391PHOVW
ZAJ3OZ4H2
GMOB9E9X7
Q8U4C8ZK1
0WDZLRWWJ
N487W3S24
PKXQFFEK3
NSMKC29IB
HOLI1T2ZB
DVPIVLLLS
FH7RSZWTM
9VSUWPZEX
NM6ZWV19I
NGN1TGF6G
JWVI7LSIZ
U99TMVXXK
KLBDMRPQV
MEFKLUO3L
LICFIK24W
ELGPLCK51
QQS4SOJV1
KJ2UVTU1B
FLQ6T7LG6
QJZLAPYN1
something like that:
<?php
$oldf=fopen('data.txt','r');
$newf=fopen('data_new.txt','w');
$i=0;
while(!feof($oldf))
{
$i++;
$line=fgets($oldf);
fwrite($newf,$line.';');
if($i%5==0)
fwrite($newf,"\n");
}
fclose($oldf);
fclose($newf);
Try this:
<?php
$file_name = "test.txt"; // Change file name as needed
$file_data = file_get_contents($file_name);
$file_data_array = explode('\n', $file_data);
$i = 0;
$new_file_data = "";
foreach ($file_data_array as $piece)
{
$new_file_data .= $piece . ';';
$i++;
if ($i % 5 == 0) // Change number of pieces you want on a line as needed
{
$new_file_data .= '\n';
}
}
file_put_contents($file_name, $new_file_data);
?>
This should do it:
$str = '2QA4ZRDUT
IDVLTLZSC
4GYC3HCMV
1W6409JD5
70P7U66TE
OG2JBBZF6
5391PHOVW
ZAJ3OZ4H2
GMOB9E9X7
Q8U4C8ZK1
0WDZLRWWJ
N487W3S24
PKXQFFEK3
NSMKC29IB
HOLI1T2ZB
DVPIVLLLS
FH7RSZWTM
9VSUWPZEX
NM6ZWV19I
NGN1TGF6G
JWVI7LSIZ
U99TMVXXK
KLBDMRPQV
MEFKLUO3L
LICFIK24W
ELGPLCK51
QQS4SOJV1
KJ2UVTU1B
FLQ6T7LG6
QJZLAPYN1';
$arr = explode(PHP_EOL, $str);
$c = 1;
$str3 = '';
foreach ($arr as $item) {
$str3.= $item.';';
if ($c % 5 == 0) {
$str3.= PHP_EOL;
}
++$c;
}
echo "<p>$str3</p>";
Be aware that you won't see the line breaks in the browser, but they will be apparent when you 'view source'.
Edit: I'm using PHP_EOL here instead of "\n" as the other answers have suggested. This ensures that the script will work correctly on any platform (Win, MacOS & *nix) as long as the PHP setting auto_detect_line_endings is set to true
I am aware that this:
<?php
$file2 = file('website/files/myfolder/file.html');
echo $file2[8];
echo $file2[9];
echo $file2[10];
echo $file2[11];
?>
would give me the contents on lines 8,9,10,11 in the file.html file, however I would like to do this for about 100 lines ranging from lines 23 to 116. How would I accomplish this without using the echo $file2[NUMBER]; one hundrend times?
Another way to explain what I am trying to do would be possibly if I could do this in php:
echo $file2[23-116];
obviously this will not work but that is the concept.
Thanks for your help!
Use a loop like so
$data = file('website/files/myfolder/file.html');
for ($i = 23; $i <= 116; $i++) {
echo $data[$i];
}
http://php.net/manual/en/control-structures.for.php
Or you could splice the data:
$data = file('website/files/myfolder/file.html');
echo implode(PHP_EOL, array_splice($data, 23, 116 - 23));
http://php.net/manual/en/function.array-splice.php
http://php.net/manual/en/function.implode.php
How about something like this?
$file = file('website/files/myfolder/file.html');
foreach( range( 23, 116) as $i) {
echo $file[$i];
}
I'm fairly new to coding and just recently started working on integrating functions into my PHP. I am trying to encode and echo an IP address to Google Analytics's. This is what my custom modifier looks like:
pagetracker._setCustomVar(1, "IP", "<?php include function.php; echo remove_numbers_advanced($_SERVER['REMOTE_ADDR']); ?>", 2);
The function file looks like this:
<?
function remove_numbers_advanced($string)
{
$numbers = array();
for($counter =0; $counter <= 10; $counter++) {
$numbers[$counter] = $counter;
$replacements = array("A","7","B","6","C","4","D","3","E","F");
$string = str_replace($numbers, $replacements, $string);
return $string;
}';
echo remove_numbers_advanced($string);
?>
When I isolated the PHP section of my custom variable in an attempt to test it the page throws a 500 error, suggesting to me that there is something wrong with how I have my script set up.
Please bear in mind I am rather new to this so simple terms and examples would help a ton!
There are few errors in your function. The correct function is:
function remove_numbers_advanced($string)
{
$numbers = array();
for($counter =0; $counter <= 10; $counter++)
$numbers[$counter] = $counter;
$replacements = array("A","7","B","6","C","4","D","3","E","F");
$string = str_replace($numbers, $replacements, $string);
return $string;
}
1- You added open curly braces next to for loop but did not close it
2- Also there is " '; " at the closing braces of function. It shouldn't be there.
include function must have a string parameter so put '' around file name
pagetracker._setCustomVar(1, "IP", "<?php include 'function.php'; echo remove_numbers_advanced($_SERVER['REMOTE_ADDR']); ?>", 2);