Can EasyPHP take command line arguments? - php

I want to write a PHP program that takes an argument from the command line but am not sure how it's done. I use EasyPHP for development, do I need something different?
In the user notes it says you can
$ php -f somefile.php a=1 b[]=2 b[]=3
But when I try something like this (even php -i) it says php is not a recognized program. What do I need to do?

I'm not familiar with easyphp, but it sounds like you need to find php.exe (probably in the folder where you installed easyphp) and add it to your Path settings in windows.

Related

Path is not available in PHP exec or shell_exec

I've been trying and searching about a small issue I have in our php application. We use a which to find the full path of a program, however it returns with
which: no bla in ((null))
on Centos, on our own debian boxes it works just fine. I've figured out since then that (obviously) the PATH is not available in the shell from PHP. But what I can't find out is why that is the case. I've replaced the command with all sorts of commands to find out what the environment is in which I run in.
If I run an echo $PATH I actually see the directories I've set in the .bash_profile. If I run echo $SHELL I know I am using bash, if I run whoami I found out I am not the apache user but a different user, whom I changed the .bash_profile for. As I've read there's a difference between shells, so I thought it might not load the .bash_profile so I've also added the export PATH to the .bashrc.
I can get it to work with a change in the code, I could replace the which, but I am just genuinely interested in why this is not working on this centos configuration. I've now added an export PATH in the exec function before the which and it works, and should also work on other systems, but still I think it shouldn't be necessary.
Anyone know what could cause this behaviour?

How do you make a Symfony command run globally

We say that I have built a really awesome Symfony console application called HelloWorld. I now want it to be available everywhere on my computer, so I can run "helloworld" and it will output "HELLO WORLD". How would one accomplish such a thing? I've thought about adding it to my aliases file but I feel like there's a better way than that.
First this has nothing to do with php, symphony2 or console. This is about OS.
You need to put that application on a folder that is in PATH or add that folder to the PATH environment variable. This is OS dependent.
For Ubuntu:
https://askubuntu.com/questions/60218/how-to-add-a-directory-to-my-path
For Windows:
"Register" an .exe so you can run it from any command line in Windows
If you're using Linux, just add an alias to it in your .bashrc file:
alias mycommand='/usr/bin/php /path/to/myapp/app/console my:command'

Double click and run the Php script on mac

I am sorry if this question was answered.
Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.
Any help would be helpful.
Try this (for mac),
Open terminal
cd to folder
Start php server - php -S 127.0.0.1:8000
Open browser and enter - http://localhost:8000/file-name.php
I think you don't understand what PHP is ...
HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.
As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.
Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:
On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:
Go into your system preferences and verify that Web Sharing is enabled.
Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.php and it will call the file YourFile.php and show you what the output of the script is.
EDIT:
If you are using PHP for scripts, like bash-scripts, see the answer #andreas-baumgart provided.
To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.
This can be done with the following steps.
Go to /etc/apache2/httpd.conf and change the permission to sudo chmod 777 httpd.conf
Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so
To start the apache built-in server, use the command sudo apachectl start in the terminal.
Now .php files can be created and run from the terminal using php -f filename.php and it can also be run on a browser using http://localhost/filename.php
You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.
To run your script on double click try this:
Make the script executable using chmod +x yourscript.php
Prepending the according Shebang to the files content: #!/usr/bin/env php.
Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".
Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.
Solution #1
The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:
#!/usr/bin/php
<?php
include('name-of-php-script.php');
?>
Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.
Solution #2
The specific inquiry requires a bit of work.
First make sure that the 1st line of the php script is:
#!/usr/bin/php
This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:
whereis php
Once you've added the Shebang line to the php script you've readied it for automatic execution.
To make it double clickable executeable you have to do the following:
Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...
Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...
OS X will verify you want it to set Terminal as the default application to open .php files
This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.
Try MAMP
MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.
Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.
You need to run the php interpreter against the file to execute on it's contents.

Append to the Apache Environment Path

I have a web accessible PHP script that is using a shell command to drop PDFs to text. I installed Poppler, and am using pdftotext, via MacPorts. I am able to run the command successfully from the CL, and when supplying the full path within the PHP script to '/opt/local/bin/pdftotext'. So, I know that my $PATH is correct and the permissions are sufficient, yet I am still getting an exit status of 127: Command Not Found, when attempting to do simply 'pdftotext' in the exec().
I have tried the answers from How do I add paths to the Apache PATH variable? and http://lists.apple.com/archives/macos-x-server/2008/Sep/msg00433.html. I modified both the /etc/paths and /etc/profile, and added /etc/paths.d/macports all pointing to '/opt/local/bin'. setenv, apache_setenv, etc all had no effect also.
I am using a MAMP (1.9 I think) install for my local development, OSX 10.6, PHP 5.3.5, all a little behind I know :-) ... my $PATH is modified to point to the MAMP bin/php
/etc/paths.d/macports will influence on PATH variable for macports, not for the Apache. You probably need to add /etc/parhs.d/apache (or else) to do what you need.
Edit: also check this and this threads for solutions. It is somewhat outdated but still can help.

Running PHP script from command line

So I'm working with a designer on a website in PHP/MySQL and there are a few scripts that he would like to have to make life easier for him. He is pretty comfortable using the command line for stuff like git, SASS, node, etc. I would like him to be able to run my scripts like he would run a program, instead of running it through PHP.
So instead of this:
php /path/to/file/create_module.php module_name
I would like to do this:
myscript create_module module_name
Is it possible to do this with PHP on an Apache server? I know I will most likely have to modify the server to interpret it properly, which is fine. I just don't even know where to begin, and couldn't find what I needed on Google.
Your best bet would to be to create an alias.
So an alias of myscript would actually point to the command: php /path/to/file/create_module.php and then any extra arguments will be passed as typed.
In command line, do the following:
cd /etc/
nano bash.bashrc
At the very bottom of the file, add this line of text:
alias "myscript=php /path/to/file/create_module.php"
BASHRC is a script that is run on user login, so the alias will be recreated every time the user logs into the system.
I am not sure what you are looking for myscript to do, but to run a php script via the command line without specifying the php binary, just add a a shebang, like
#!/bin/env php
<?php
// The above expects env is in /bin
$foo = "bar";
Or the full path if you like
#!/usr/bin/php
Another option: Shell script wrapper
create a new text file in /bin or another directory in your PATH, name it how you would like to invoke your script and give it this content
#!/bin/bash
cd /path/to/your/php/scripts/folder
php script.php $*
don't forget to
chmod a+x /path/to/bash/script
The advantage of this is that your PHP script is run in the right directory where it may expect other resources to be that it depends on.
On A Linux Solution:
/usr/bin/php /path/to/file.php
On a Windows Solution:
C:\Path\To\PHPExe C:\Path\To\phpfile.php

Categories