Passing of variables outside the loop in PHP - php

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()) ?>

Related

how to use variable variables PHP function on my code?

Normally i use this code and it's will be echo work good.
<?PHP
echo $row->test_column_1;
?>
But when i tyied to use this code, it's not echo any data. How can i do ?
<?PHP
$i = "1";
echo ${'row->test_column_' . $i};
?>
You can access a dynamic property name like this:
<?php
$i = "1";
echo $row->{'test_column_' . $i};

Reading RSS feed

I need to read an XML file, i watched some tutorials and tried different sollutions, but for some reason I can't figure out why it doenst work.
The XML file that I want to read: http://www.voetbalzone.nl/rss/rss.xml
This is the code that im using:
$xml= "http://www.voetbalzone.nl/rss/rss.xml"
for ($i = 0; $i < 10; $i++)
{
$title = $xml->rss->channel->item[$i]->title;
}
The error I get: Premature end of data in tag
It works for me like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
for ($i = 0; $i < 10; $i++)
{
$title = $xml->channel->item[$i]->title;
}
?>
Note that you are overwriting the variable $title each time, so that you will have the title of the 10. element in it after the loop finished [I assume that is not what you want?]
To get all 'item'-Elements inside 'channel' as an Array to iterate through you can use xpath like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
$item_array = $xml->xpath("//rss/channel/item");
foreach($item_array as $item) {
echo $item->title . "\n";
}
?>
I would suggest to read about php's SimpleXML here: http://php.net/manual/en/book.simplexml.php

php outcome of sum multiply with html element

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

Taking data from multiple forms in a loop and insert into database in a loop

I have a form in a loop that iterates 4 times. I want to process and print the form data dynamically.
This is not showing the required output. Am I doing something wrong ?
index.php
<?php
for($i = 1; $i<5; $i++)
{
?>
<form action = 'index.php' method = 'post'>
<input type = 'text' name = 'name<?php echo $i; ?>'>
<?php
}
?>
<input type = 'submit' value = 'submit'>
<?php
for($i = 1; $i<5; $i++)
{
$namee = $_POST['name.$i'];
echo $namee;
}
?>
The problem is in this line :
$namee = $_POST['name.$i']; //you cannot concatenate variables this way,
it should be (note where the single quote is):
$namee = $_POST['name'.$i];
Single quotes not evaluate variables, you can use concatenation:
$namee = $_POST['name'.$i];
Or double quotes
$namee = $_POST["name$i"];

PHP GET method doesn't work in Xampps

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';
}

Categories