While it seems like there are multitude of different methods to execute commands in PHP, so far the code I will go over below is my preferred method. Using the passthru() function works nicely and includes the final return code of whatever was being executed, which is generally useful to me in error checking. In processing, I also include ob_start(), ob_get_contents(), and ob_end_clean() so that I can capture the console programs output and return it the user eventually.
//execute a console command and return both the commands
//success value and any output to the console it made
function executeConsoleCommand($commandToExecute, &$commandConsoleOutputToAppend)
{
$returnValue = 0;
$commandConsoleOutputToAppend .= $commandToExecute . "\r\n";
//catch console output
ob_start();
//execute the console program
passthru($commandToExecute, $returnValue);
//save console output to a string variable
//(this should APPEND to the variable)
$commandConsoleOutputToAppend .= ob_get_contents() . "\r\n";
ob_end_clean();
//add this data to the log file
updateLogFile($commandConsoleOutputToAppend, "a");
return $returnValue;
}
Take note that in my function, I append all of the console output to a parameter variable using pass by reference. The console program’s return code is passed back through the standard return variable of the function.
I’m the type of person who prefers to do things in a way that I perceive as the proper or correct way. While this tends to make things take a bit longer to accomplish, I feel the result ends up being better overall. One such thing I noticed is that file and path handling in PHP was missing a function I used quite a bit when writing C# code. In C# there is a Path.Combine() function that appends two file paths together to form one complete path without having to care what the beginning character and ending character of the two paths are. I wrote up a little function in PHP to mimic what happens.
//take a path and/or a filename and combine them into one file path
function filePathCombine($path1, $path2)
{
$completedPath = '';
if(substr($path1, strlen($path1) - 2, strlen($path1) - 1) !== DIRECTORY_SEPARATOR)
{
$completedPath = $path1 . DIRECTORY_SEPARATOR;
}
else
{
$completedPath = $path1;
}
if(substr($path2, 0, 1) !== DIRECTORY_SEPARATOR)
{
$completedPath .= $path2;
}
else
{
$completedPath .= substr($path2, 1, strlen($path2) - 1);
}
return $completedPath;
}
Looking through the code you will notice DIRECTORY_SEPARATOR, which is a global PHP constant. While it isn’t needed considering using / instead of \ would work on Windows and Linux, I thought it best to try to stay with my idea on how to do things the proper way.
I’m writing some software that relies on a feature of GDAL (Geospatial Data Abstraction Library) in a more recent version than the one Ubuntu Lucid currently has in their repository. As I write the software on my desktop, I found out pretty quickly that I was using an old version of one of the commands (more specifically ogr2ogr and the clipping functionality).
Thanks to this site for the initial tip, it’s pretty easy to get the latest “unstable” release of GDAL installed without having to manually compile the program from source code yourself.
If you are using Karmic or above, you can run these commands:
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
Or you can opt for the more involved version below:
In your desktop, navigate to here:
System -> Administration -> Software Sources
“Other Software” tab
Add these two items:
deb http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntumain deb-src http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu main
Where in my case,
You will also need to run from a terminal:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 314DF160 sudo apt-get update
If you don’t have GDAL installed, you can install it from the terminal by:
sudo apt-get install gdal-bin python-gdal
“python-gdal” being optional…
Otherwise, you can use the standard Ubuntu update program to install the newest version on your computer:
System -> Administration -> Update Manager
Run through that and you should see a new version of GDAL show up to be installed. Install those updates to get the newest version!
Java Netbeans is a pretty decent editor for PHP code. I use Linux Ubuntu now for my primary desktop computer, so it’s quite convenient and easy to get running.
One issue is that in default configuration the program is a bit slow feeling.
Doing a quick search I found this page:
http://performance.netbeans.org/howto/jvmswitches/
Which describes a few command line parameters that might help the IDE function faster.
I’m currently running the command “/usr/bin/netbeans -J-Xverify:none -J-Xmx384m -Dsun.java2d.opengl=true”
Basically, it removes one unnecessary verification check, adds 256mb of RAM to the default RAM pool the program can use, and attempt to use OpenGL to render the IDE. I am currently using closed-source video card drivers, so your mileage could very with that last parameter.
You can modify your default system menu link by doing this:
Right-click on the “Applications” dropdown system menu. Select the “Edit Menus” list item.
Click the Programming menu and then the Netbeans X.X icon. There will be a Properties button where you can modify the command being called. Just add the parameters you would like to try out in there.
Overall I feel a noticeable improvement in scrolling, auto-completion pop-ups, and switching between tabbed documents with the limited bit of testing I’ve done so far.