Run function php in console phpStorm? - php

I'm a new php and I'm using phpStorm to develop, there is anyway can run a function in terminal (console) in phpStorm?
For example I can type in terminal (console):
> php function.php getProduct();

There's no way to run a function inside a file with syntax like that out of the box. (You could write code which evaluates the arguments passed via the CLI and make it execute that function, but I don't think that's what you're interested in.)
You'll have to write a PHP script which imports and executes the function. You can do that ad-hoc on the command line, e.g.:
$ php -r 'require "function.php"; print_r(getProduct());'
Or you go into interactive mode and do the same there:
$ php -a
php > require 'function.php';
php > print_r(getProduct());

On Mac, you can open a shell terminal by going to the Menu Bar, then View --> Tool Windows --> Terminal. If you want to start a PHP console inside the terminal window, run php -a. See here for more information on the interactive PHP terminal.
Edit
It seems that the root cause of this not working is that you're running in a Windows environment and php.exe is not in your command PATH. See here for instructions on adding php.exe to your PATH. If you run into problems, there is some additional information in the comments on the linked page. Here are the instructions for Windows 7, XP, Vista, 2008, 2012 and up:
Go to Control Panel and open the System icon (Start → Control Panel)
Go to the Advanced tab
Click on the 'Environment Variables' button
Look into the 'System Variables' pane
Find the Path entry (you may need to scroll to find it)
Double click on the Path entry
Enter your PHP directory at the end, including ';' before (e.g.
;C:\php)
Press OK

Related

Command 'php' not recognized but it is registered in Windows PATH

I have "C:\Ampps\php" setup in the PATH string on my Windows server but when I type 'php' on the PHPStorm terminal I'm always given 'php' is not recognized as an internal or external command, operable program or batch file.
Is this a bug with PHPStorm 7.1.3? It works from the command line if I open a new Command Prompt window externally.
For any new readers facing such problem in PhpStrom just set path directly from PhpStrom terminal
set path=%path%;c:/xammps/PHP
Not recommended to use PHP from xammps or other such programs since it will be your working PHP directory and if it gets deleted then you have to perform another task to handle it or some errors might pop up in windows just install a fresh version of PHP lots of tutorials are out there for Windows.
But nevertheless to use PHP in PhpStrom Terminal from xammps just run that command it will set the path that PhpStrom can find.
I had the same problem. I have "C:\__OPENSERVER__\modules\php\PHP-7.2-x64" setup in the PATH.
To solve it I added in PhpStrom terminal and executed:
set path=%path%;C:\__OPENSERVER__\modules\php\PHP-7.2-x64
open setting in file menu
find tools tab in this window
click on terminal
in project setting click on environment variable
add php path (for example "C:/xampp/php") to this and save
restart phpstorm

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.

Run .php file in Windows Command Prompt (cmd)

I want to run a .php file in windows cmd.
I followed this (suggestion)
php is not recognized as an internal command (in Windows)
But it did not work.
I tried this command prompt: C:\Windows\system32> cd \myfolder
And within myfolder I did as follows:
C:..\myfolder> php file.php
but I get an error "php is not recognized as internal or external"
But when I try without php command like this; C:..\myfolder> file.php
it just opens in notepad, yet I want to run it in cmd. How can I do it?
If running Windows 10:
Open the start menu
Type path
Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.
If on older Windows:
Show Desktop.
Right Click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the System Properties window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
That should work.
Note: Make sure that your PHP folder has the php.exe. It should have the file type CLI. If you do not have the php.exe, go ahead and check the installation guidelines at - http://www.php.net/manual/en/install.windows.manual.php - and download the installation file from there.
you can for example: set your environment variable path with php.exe folder e.g c:\program files\php
create a script file in d:\ with filename as a.php
open cmd: go to d: drive using d: command
type following command
php -f a.php
you will see the output
You should declare Environment Variable for PHP in path, so you could use like this:
C:\Path\to\somewhere>php cli.php
You can do it like this
It seems your question is very much older. But I just saw it.
I searched(not in google) and found My Answer.
So I am writing its solution so that others may get help from it.
Here is my solution.
Unlike the other answers, you don't need to setup environments.
all you need is just to write php index.php if index.php is your file name.
then you will see that, the file compiled and showing it's desired output.

Git bash on windows 7. mysqldump command is not working

I'm using git bash on a windows 7 machine. When I try to use mysqldump I'm getting an error:
"sh.exe": mysqldump: command not found". I am very new to this. Can somebody help me?
You'll need mysqldump on the path that bash is using to find commands. UN*X shells, like bash, and also windows cmd, use a PATH environment variable to manage this.
To see your current path, type this in bash:
$ echo $PATH
If you've installed MySQL, it's typically located at c:\Program Files\MySql, and the support programs, like mysqldump, are in c:\Program Files\MySql\MySQL Server 5.5\bin\. You'll need to have that directory on your path. If MySQL has been installed somewhere else on your machine then you'll need to find the bin directory there and use that path in these instructions.
The best way to do this in windows is by changing the user or system environment variables. To do this, in Windows Explorer:
Right-click Computer and select Properties from the menu.
In the resulting window, choose Advanced System Settings in the sidebar.
In the resulting dialog, click the Advanced tab.
Near the bottom, click the Environment Variables button.
There are 2 sections, user at the top, and system below. Either will work, but let's change the system settings.
Scroll the list until you see the variable named Path in the Variable column.
Double-click this Path row.
In the resulting dialog, in the Variable value field, scroll all the way to the end of the field, and at the end, add this: ;c:\Program Files\MySql\MySQL Server 5.5/bin/. Note the leading semicolon: it won't work without it.
Click OK and now you can close all the dialogs/windows.
Once you've done all that, close the Git bash window, and open another one. Type:
$ mysqldump
You should get Usage instructions from the program.
If not, look at the path again by typing:
$ echo $PATH
Do you see anything like what you added? Any MySQL directories there? It's likely you've just mistyped something, or missed the semicolon. Go over the steps again and make sure everything is correct.

php error : php command not found

My system is win7. I have installed XAMPP on the system successfully.Besides, I have set the environment variables:
PHP_HOME=C:\xampp\php
Path=%PHP_HOME%
But when i run php code in git bash ,it appears the error:"sh.exe:php:command not found"
How can i solve the problem?
In my case I can solved my problem in this way ...
Go to PHP directory in the XAMPP folder and run the code in this way with the XAMPP server:
$ Php /c/xampp/htdocs/cupon/bin/vendors install
As far as i was trying to execute MSVC compiler from within Java, i got the idea of all that Win7: the shell you are running something has different environment variables that you think you've set.
I recommend you to insall WAMP server - it is totally compatible with Windows while XAMPP seems to be a bit hard way to start with.
And one more hint: try running php / sh from the bin/ PHP directory.
If you're using Windows and getting the "php: command not found" error, it means that the PHP executable is not in your system's PATH environment variable. Here are the steps to fix it:
Find the location of your PHP installation. By default, PHP is installed in the "C:\php" directory. If you've installed PHP in a different location, note down that path.
Add the PHP directory to the system's PATH environment variable:
Open the Start menu and search for "Environment Variables."
Click on "Edit the system environment variables."
Click on the "Environment Variables" button at the bottom of the window.
Under "System Variables," scroll down and find the "Path" variable. Click "Edit."
Click "New" and enter the path to your PHP directory (e.g., "C:\php").
Click "OK" to close all the windows.
Close and reopen your terminal or command prompt to apply the changes.
Verify that PHP is working by running the command "php -v" in your terminal or command prompt. You should see the version of PHP installed on your system.
If PHP is still not working after following these steps, you may need to reinstall PHP or check that the PHP executable file is in the correct directory.

Categories