I have trouble with php variable [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a little question about variables in php.
How can I make this possible??
<?php
echo $test;
$test = 'This is a test';
?>
I know that you could fix it with ease with
<?php
$test = 'This is a test';
echo $test;
?>
but I cant use it in my page in that way.
Can any one please tell me how I can have a variable after the echo??

Buddy.. It can't be Done.. How you can output a value of a variable when its not assign to it..
Its common sense..

<?php
echo getTest();
function getTest(){
return 'This is a test';
}

You can't. A variable has to be declared before it has a value.

Related

Php function with return statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Can somebody explain me this code? I don't understand why it outputs 21.
<?php
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
?>
It will echo 21. I have no idea how it got this result.
The function is recursive, it calls itself until it gets to 0, then adds all the previously returned values (6,5,4,3,2,1).
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
So on loop one it gets 6 then 6-1 = 5 so math gets called again with 5 this time and so on. Take a look at http://sandbox.onlinephpfunctions.com/code/e228f3b696c5058efee03fa978a09179c1f2ffbb.

PHP HTML {$variable} [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can i print variable in brackets? Like ip board, for example I have this code:
<?php
$variable = "test";
?>
<div class="test">{$variable}</div>
How can I do this? Not
echo $variable;?>
but {$variable} ! Thanks in advance :3
It's called concatenation and it uses '.' to string things together...
<div class="test"><?php echo '{'.$variable.'}' ?></div>
you can use it like this
<?= $variable ?>
Use a php templating engine. Best for you is that use Laravel and Balde
yes, you can just do it like
<?php
$variable = "test";
?>
<div class="test"><?= $variable; ?></div>
but if you want to use above approach(your one) you've to use blade templating engine
have a look

line break conversion from html to php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have html code loaded in array $gh[0] and i am coonverting it to plain text
if i compare with same text loaded into new variable it doesn't works
can someone help with the issue, what should be exact value in $a to match the content in $gh[0]
i want comparsion to be working , what will be output if $gh[0] is converted into plain text especially line break
<?php
$gh[0]="Net inventory (used in)/from<br>Production Activities"
$fg=$gh[0]->plaintext;
$a="Net inventory (used in)/from Production Activities"
if($a == $fg)
{
echo "match";
}
else
{
echo "No match";
}
?>
What about just stripping all elements from the input?
$gh[0]= strip_tags($gh[0]);
If you want to know what the value (of $fg) is, why don't you check it?
There's echo, print_r and var_dump.

How to format a string returned by a PHP function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wrote a program that uses an ISBN to query Amazon for that ISBN's price, and returns the following result:
5cb66919-8a8d-4c13-9175-790f0476508d0.0401580000000000TrueISBN9780340979266OffersAllAll0340979267809USD$8.09688USD$6.88598USD$5.98241010000
I need to format this so that I have the price information in a format that would be understandable to a human.
How can I do this?
Use this way..
<?php
$s = '5cb66919-8a8d-4c13-9175-790f0476508d0.0401580000000000TrueISBN9780340979266OffersAllAll0340979267809USD$8.09688USD$6.88598USD$5.982410100000';
$p = explode('USD',$s);
echo "<pre>";
print_r($p);
echo "</pre>";
?>
function returnPrice($response){
$prices = explode('USD',$response);
unset($prices[0]);
return $prices;
}

Is it possible to combine array variable with array number to complete 1 variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My code is as below :
assume that $a is a variable that 1 take out from DB and now i wan to combine it with an array number and produce an output that same with $box. Is it possible to do that ? or i m wrong ? please guide me.. thanks
$b="1-1-0-1";
$box=explode("-",$b);
$a="$box"; //from DB
echo $box[2];
echo "$a[1]";
Remove the quotes. Why are you so obsessed with them? https://stackoverflow.com/users/2892997/user2892997
$a=$box; //from DB
echo $box[2];
echo $a[1];
$b="1-1-0-1";
$box=explode("-",$b);
$a='$box'; //from DB
$a = substr($a, 1);
echo $box[2];
$c = $$a;
print_r($c);

Categories