• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 831
  • Last Modified:

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.
0
jeff_zucker
Asked:
jeff_zucker
2 Solutions
 
GaryCommented:
Have you declared $str before trying to append to it.
0
 
asavahCommented:
thats an example of  how I did it
exec("/var/web_sh/somescript.sh",$result);
foreach ($result as &$value) {
        echo "$value</br>";
}

Open in new window


Try it this way if it works for you just change echo "$value</br>"; for anything that suits your needs.
0
 
Dave BaldwinFixer of ProblemsCommented:
The back tick " ` " is not listed as a valid quote character on http://www.php.net/manual/en/language.types.string.php .
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mark BradyCommented:
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.
0
 
Lukasz ChmielewskiCommented:
Remember to use
error_reporting(E_ALL) at the top of your script, might come in handy sometimes.
0
 
jeff_zuckerAuthor Commented:
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?
0
 
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_jeff_zucker.php

Outputs:
Notice: Undefined variable: str in /home/websitet/public_html/RAY_temp_jeff_zucker.php on line 11
string(0) ""
Notice: Use of undefined constant FOO - assumed 'FOO' in /home/websitet/public_html/RAY_temp_jeff_zucker.php on line 15
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);

Open in new window

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);

Open in new window

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
0
 
jeff_zuckerAuthor Commented:
I ended up changing to the exec command instead of the backtick operator.  It looks like this:
exec ("<command>", $str1, $str2);
I just check if $str2 is great than zero to see if there is an error.

Giving points to asavah as seeing the way to get results through exec is what got me on the right track.
0
 
jeff_zuckerAuthor Commented:
See my comment for more information.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now