Hey i have problem code:
$$videoList[$i]["id"] call variable -> $hello
but i need add one more letter to the end
$$videoList[$i]["id"]+W call varriable -> $helloW
<p>'.$$videoList[$i]["id"].'</p>
I don't advise to do what you are doing.
The code becomes a true hell that nobody can mantain in the long run.
Anyway, if you hate the world, you can achieve with ${$videoList[$i]["id"].'W'}
As for a better understanding, see this example:
<?php
$hello = 'sayHello';
$helloW = 'with W';
$myVar = 'hello';
echo $myVar; //hello
echo $$myVar; //sayHello
echo $$myVar.'W'; //sayHelloW
echo ${$myVar.'W'}; //with W
Related
Im a student studying PHP, Java. but I don't know if eval can access outside variable.
My code :
<?php
$o = "This is just test character";
$code = "echo $o;"
eval($code);
?>
does it active? if not, how can i make it to active? (my purpose is eval() can access outside variable..)
sorry for my bad english and thx for listening.
Apart from the obvious missing ; on line 2
The issue you're having with eval is the string you're passing is not valid PHP.
<?php
$o = "This is just test character";
$code = '<?php echo $o;';
eval('?>'.$code);
https://3v4l.org/CHkR3
And if you're using double quotes it will parse into the string and again create invalid PHP by missing the quotes.
<?php
$o = "This is just test character";
$code = "<?php echo '$o';";
eval('?>'.$code);
https://3v4l.org/CHkR3
Bottom line, if you don't know how eval works then you should definitely not use it.
You need to put the string in the $code variable between aposthropes and then it'll work. Like this:
$o = "'This is just test character'";
$code = "echo $o;";
eval($code);
I have the following php variables in my code:
$masonry_item_width;
$masonry_item_padding;
I am trying to create a value for a new variable by doing soe maths with these two. Here is what I've written:
$skizzar_double_width_size = eval(
'return'.($masonry_item_width*2)+$masonry_item_padding.';'
);
However, this returns nothing when I echo out $skizzar_double_width_size
I've never done maths with php variables before, is there something i'm doing obviously wrong?
Try simply with this:
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
To echo it out to HTML just write:
echo $skizzar_double_width_size;
And yes, it's that simple! :)
You don't have to use eval() just use this:
echo $skizzar_double_width_size = $masonry_item_width * 2 + $masonry_item_padding;
BTW: eval() === evil(), i have never seen a situation where you need eval() it just makes you problems
I usually run code like this just fine:
$ZANE_REGISTER_RULES='this wont print';
myTest();
function myTest()
{
**global $ZANE_REGISTER_RULES**;
$ZANE_REGISTER_RULES='this will actually print';
}
echo $ZANE_REGISTER_RULES; //will print "this will actually print"
But sometime (eg: if I put this inside a phpBB page) this doesn't work (the echo says "this wont print") unless I declare the variable global the first time too:
**global $ZANE_REGISTER_RULES**;
$ZANE_REGISTER_RULES='my rulessssssssssssssss';
myTest();
function myTest()
{
**global $ZANE_REGISTER_RULES**;
$ZANE_REGISTER_RULES='funziona';
}
echo $ZANE_REGISTER_RULES; //will print "this will actually print"
I'm pretty sure that the first way is the correct one and the second one just doesn't mean anything, nevertheless the second one works, the first one doesn't.
PLEASE don't waste time replying "global are bad programming" because this is not the issue at hand, neither "why would you do such a thing?" because this is obviusly an example.
There is only one reason why this might be happening: the code in the second example is being compiled in the context of a function. That's why $ZANE_REGISTER_RULES has local scope by default.
If there is no enclosing function in the source file where the code itself appears, this means that the file is being included by some other file inside a function context, for example:
var_access.php
echo "Hello ".$name."\n";
echo "Hello ".$_GLOBALS['name']."\n";
test_1.php
// Here var_access.php is included in the global context
$name = 'world';
include('var_access.php'); // Prints "Hello world" twice
test_2.php
// Here var_access.php is included in a function context
$name = 'world';
function func() {
$name = 'function world';
include('var_access.php'); // Prints "Hello world" and "Hello function world"
}
I would like to know if it's possible to execute the php code in a string. I mean if I have:
$string = If i say <?php echo 'lala';?> I wanna get "<?php echo 'dada'; ?>";
Does anybody knows how?
[EDIT] It looks like nobody understood. I wanna save a string like
$string = If i say <?php count(array('lala'));?>
in a database and then render it. I can do it using
function render_php($string){
ob_start();
eval('?>' . $string);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
The problem is that I does not reconize php code into "" (quotes) like
I say "<?php echo 'dada'; ?>"
$string = ($test === TRUE) ? 'lala' : 'falala';
There are lots of ways to do what it looks like you're trying to do (if I'm reading what you wrote correctly). The above is a ternary. If the condition evaluates to true then $string will be set to 'lala' else set to 'falala'.
If you're literally asking what you wrote, then use the eval() function. It takes a passed string and executes it as if it were php code. Don't include the <?php ?> tags.
function dropAllTables() {
// drop all tables in db
}
$string = 'dropAllTables();';
eval($string); // will execute the dropAllTables() function
[edit]
You can use the following regular expression to find all the php code:
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
$php_code will be an array where $php_code[0] will return an array of all the matches with the code + <?php ?> tags. $php_code[2] will be an array with just the code to execute.
So,
$string = "array has <?php count(array('lala')); ?> 1 member <?php count(array('falala')); ?>";
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
echo $php_code[0][0][0]; // <?php count(array('lala')); ?>
echo $php_code[2][0][0]; // count(array('lala'));
This should be helpful for what you want to do.
Looks like you are trying to concatenate. Use the concatenation operator "."
$string = "if i say " . $lala . " I wanna get " . $dada;
or
$string = "if i say {$lala} I wanna get {$dada}.";
That is what I get since your string looks to be a php variable.
EDIT:
<?php ?> is used when you want to tell the PHP interpreter that the code in those brackets should be interpreted as PHP. When working within those PHP brackets you do not need to include them again. So as you would just do this:
// You create a string:
$myString = "This is my string.";
// You decide you want to add something to it.
$myString .= getMyNameFunction(); // not $myString .= <?php getMyNameFunction() ?>;
The string is created, then the results of getMyNameFunction() are appended to it. Now if you declared the $myString variable at the top of your page, and wanted to use it later you would do this:
<span id="myString"><?php echo $myString; ?></span>
This would tell the interpreter to add the contents of the $myString variable between the tags.
Use token_get_all() on the string, then look for a T_OPEN_TAG token, start copying from there, look for a T_CLOSE_TAG token and stop there. The string between the token next to T_OPEN_TAG and until the token right before T_CLOSE_TAG is your PHP code.
This is fast and cannot fail, since it uses PHP's tokenizer to parse the string. You will always find the bits of PHP code inside the string, even if the string contains comments or other strings which might contain ?> or any other related substrings that will confuse regular expressions or a hand-written, slow, pure PHP parser.
I would consider not storing your PHP code blocks in a database and evaluating them using eval. There is usually a better solution. Read about Design Pattern, OOP, Polymorphism.
You could use the eval() function.
I'm new to PHP and web scripting in general so this a newb question.
Currently i'm a creating an instance to an object, yet when I call the constructor
the script slienty shuts down... it doesn't call the next function and I don't know why.
Any help would be welcome.
Here is the code.
<?php
class product {
var $ssProductName;
var $ssVendorName;
var $ssDescr;
var $ssURI;
// Clean constructor, strings must be cleaned before use
function __construct($ssProd, $ssVendor, $ssD, $ssU) {
$this->$ssProductName = $ssProd;
$this->$ssVendorName = $ssVendor;
$this->$ssDescr = $ssD;
$this->$ssURI = $ssU;
}
// print a table of the values
function DisplayOneEntry() {
echo '<table border="1">
<tr>
<td>'.$this->$ssProductName.'</td>
<td>'.$this->$ssVendorName.'</td>
<td>'.$this->$ssDescr.'</td>
<td>'.$this->$ssURI.'</td>
</tr>
</table>';
}
}
echo "<HTML>";
echo "A";
$newP = new product("Redhat", "Redhat corp", "Leader in", "www.redhat.com");
echo "B";
$newP->DisplayOneEntry();
echo "</HTML>";
?>
But the output is just:
<HTML>
A
Then nothing else.
This is running on a hosting provider using php 5.2.9 and Apache 2.2.
You need to access the member variables with:
$this->variableName
Not:
$this->$variableName
$this->$ssProductName = $ssProd;
should be
$this->ssProductName = $ssProd;
no $ after the ->
The syntax $this->$foo is a variable variable referencing the class attribute with the name of the value of $foo. So if $foo has the value bar, $this->$foo would reference $foo->bar and not $this->foo.
So just remove the $ after $this-> and it should work.
That is because your php script is erroneous. To catch such errors, you should run it on a debugging server (with display_errors set to On) or use other logging methods.
However, the main problem is you are accessing object members the wrong way; there's no second dollar sign. Instead of
$this->$ssProductName = $ssProd;
use
$this->ssProductName = $ssProd;