jeff_zucker
asked on
How do I get output from backtick operator in php file
I have a php file that executes some commands. It looks something like this:
while (some condition) {
$str .= `some command`;
}
I tried to echo $str and it was null, despite several errors that should have been reported. What am i doing wrong? I want a list of errors held in a variable so that I can process it in my code.
Not sure if it matters, but I have a LAMP configuration.
while (some condition) {
$str .= `some command`;
}
I tried to echo $str and it was null, despite several errors that should have been reported. What am i doing wrong? I want a list of errors held in a variable so that I can process it in my code.
Not sure if it matters, but I have a LAMP configuration.
Have you declared $str before trying to append to it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The back tick " ` " is not listed as a valid quote character on http://www.php.net/manual/en/language.types.string.php .
GaryC123 : No need to declare a variable before appending to it. Php considers the variable as type (whatever is appended to it or assigned to it).
Jeff: Dave is right, back tics are not used in php as string deliminators. They are used in mysql in that fashion but you need to change your code to this..
while (some condition) {
$str .= 'some command';
}
Depending on what final result you want, you may need to separate each appended string with a comma or a space or some type of separator so you can easily explode it into an array if need be.
Jeff: Dave is right, back tics are not used in php as string deliminators. They are used in mysql in that fashion but you need to change your code to this..
while (some condition) {
$str .= 'some command';
}
Depending on what final result you want, you may need to separate each appended string with a comma or a space or some type of separator so you can easily explode it into an array if need be.
Remember to use
error_reporting(E_ALL) at the top of your script, might come in handy sometimes.
error_reporting(E_ALL) at the top of your script, might come in handy sometimes.
ASKER
Trying a modified version of asavah's solution tonight. I will report back tomorrow with the results.
The reason I was using the backtick operator was to execute commands. I was hoping the output would be written to $str, but I guess not. So exec is better than the backtick operator for executing commands?
The reason I was using the backtick operator was to execute commands. I was hoping the output would be written to $str, but I guess not. So exec is better than the backtick operator for executing commands?
Please see http://www.laprbass.com/RAY_temp_jeff_zucker.php
Outputs:
Notice: Undefined variable: str in /home/websitet/public_html /RAY_temp_ jeff_zucke r.php on line 11
string(0) ""
Notice: Use of undefined constant FOO - assumed 'FOO' in /home/websitet/public_html /RAY_temp_ jeff_zucke r.php on line 15
string(3) "FOO"
Regarding the use of backticks in PHP (as opposed to MySQL, where they have a different meaning and use case), this page explains what might or might not be happening, depending on certain server settings.
http://php.net/manual/en/language.operators.execution.php
On my server this works correctly:
Thanks and regards, ~Ray
Outputs:
Notice: Undefined variable: str in /home/websitet/public_html
string(0) ""
Notice: Use of undefined constant FOO - assumed 'FOO' in /home/websitet/public_html
string(3) "FOO"
<?php // RAY_temp_jeff_zucker.php
error_reporting(E_ALL);
/* PROBLEM DEFINITION
while (some condition) {
$str .= `some command`;
}
*/ // END PROBLEM DEFINITION
// IN A VACCUUM
$str .= `some command`;
var_dump($str);
// ANOTHER TEST
$str .= FOO;
var_dump($str);
Some discussion may be in order. First, the idea about using error_reporting(E_ALL); is a requirement, not an option. Without the highest error reporting levels, you cannot see what PHP is doing with undefined variables and constants, so a simple typographical error can leave you searching for problems that would otherwise be immediately obvious and easy to fix.Regarding the use of backticks in PHP (as opposed to MySQL, where they have a different meaning and use case), this page explains what might or might not be happening, depending on certain server settings.
http://php.net/manual/en/language.operators.execution.php
On my server this works correctly:
<?php
error_reporting(E_ALL);
$str = `dir`;
var_dump($str);
So my suggestion would be to test this last little script and see what you get. If you get a directory listing, you know the backtick notation is available on your server. If you do not get a directory listing, please post back here to show us what you got instead, and we can move on from there.Thanks and regards, ~Ray
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
See my comment for more information.