How to run PHP from command line - php

I am trying to create a cron job that will back up my MySQL database. However it doesn't seem to ever call it.
I then tried to check if I could even run it from the terminal and I can't do that either. What is the problem?
I don't understand what the PATH variable means, I guess it must be something to do with that?
I'm just running php /var/www/html/backup.php

If php installed , go to the specified file location (cd to the file location in terminal) and enter below
php exampleFile.php

You need to install php if it's not there.
Try apt-get install php for debian http://php.net/manual/en/install.unix.debian.php
I've you've got it installed, try to use the full path to executable:
/usr/local/bin/php /var/www/html/backup.php
(Replacing /user/local/bin with the actual path to the php executable)

Related

Install phpunit on windows without composer won't work

I'm trying to install phpunit manually on my windows machine. I don't use composer, so I have to install it manually with a .phar file. I'm following the instructions from the official documentation:
https://phpunit.readthedocs.io/en/latest/installation.html#windows
I've done every step, but it won't work. phpunit --version will throw me the message 'phpunit' is not recognized as an internal or external command, operable program or batch file. Can someone Help me?
I'm using Windows 7 64-bit with XAMPP 3.2.2 and PHP 7.1.1.
EDIT:
My phpunit.cmd file location is C:\xampp\php\phar\phpunit.cmd.
The file contains the code #php "%~dp0phpunit.phar" %*. I've added ;C:xampp\php\phar to my PATH variable.
In order to make Windows recognize any command, you must put the binary's path in the relevant environment variable (PATH). Before that you need to create some kind of file Windows can run natively (i.e. not a .phar – can be a command line / batch file or executable). Here are instructions:
https://phpunit.de/manual/current/en/installation.html#installation.phar.windows
Edit: If following the above instructions, it's also important to make sure that php itself is part of the PATH environment variable, so that it's possible to execute commands like php phpunit.phar.

How to configure php in netbeans so that it's pointing to "opt/lampp/bin/php" instead of "/usr/bin/php"?

I'm using linux and installed xampp that already provided php in its package. Then, I add "export PATH=/opt/lampp/bin:$PATH" to my "~/.bashrc" so that I can use every commands that is provided in xampp directly from my terminal (without needed to pointing to "/opt/lampp/bin"). Everything works fine.
My problem is I'm using netbeans and when I tried to execute my program, it said "/usr/bin/env: php: No such file" I know it happened because there's no php in my "/usr/bin" since I have it via xampp, but I do have it in my "/opt/lampp/bin". How to configure PHP in netbeans so that it's pointing to "/opt/lampp/bin", instead of "/usr/bin"?
I've googled it and they said that I need to install php-cli, which I think is not a good choice since it makes me have two php in my system. IS there any way for me to use netbeans and php from xampp without needed to install php-cli?
I've also tried to Tools->Options->PHP-General and provide PHP interpreter poiting to "/opt/lampp/bin/php" but it didn't work.
Have you e.g. restarted OS after editing .bashrc file? Or run in command line
. ~/.bashrc
to apply changes in bashrc and then start NetBeans and give it another try. Another option could be to use symbolic link
sudo ln -s /opt/lampp/bin/php /usr/bin/php
If you run "which php" does it point to the lampp directory?

php compile less file using lessc via nodejs on windows 7

I am trying to compile less file using php, and I am trying to use lessc which I installed as nodejs module on my windows 7
I am trying to do :
$command = lessc %s -compress %s
via
exec($command, $output);
and if I do this via cmd it works fine but I can't execute it using above command in php :(.
the path which works are relative to my drive (E:/wamp/my-project/less/hello.less)
Any body knows what I am doing wrong?
Note: I know how I can use lessphp compiler, and I am already using it but I would like to use lessc module installed via nodejs.
Are you able to get the output of the command when it fails? It's possible that lessc isn't directly available in your path. If you installed it via npm install -g then it will likely be in %appdata%, which is a user path'd directory. It really depends upon how the php process gets initiated, but my guess is that this path won't work.
If I need to use an npm module like this, I usually npm install it locally, and invoke the path to lessc.cmd from a relative path to my program.
Hope this helps. Happy coding!

Windows Command Line - Multiple PHP Versions

I am currently running Wampserver with multiple PHP versions (5.3.8, 5.4.3). Wampserver easily allows you to switch back and forth between the php version you want apache to use. However, I'm having trouble dealing with multiple versions from the CLI. I have projects that require the command line, but some are compatible with php5.4, while some are not.
Is there a way to create some type of "alias" in Windows that allows me to specify which version of PHP to use on the command line .. I.E: "php54 cache:clear", "php53 cache:clear" ??
Thanks!
I am doing something like below.
ex. I have two php version , PHP 5.6 and PHP 7.3
I have wamp environment so I have done as below
so I have copied D:\wamp\bin\php\php7.3.6\php.exe to D:\wamp\bin\php\php7.3.6\php7.exe
and I have copied D:\wamp\bin\php\php5.6\php.exe to D:\wamp\bin\php\php5.6\php56.exe
Then I have added both to environment variable path like below.
now you need reopen the cmd and you can use command something like below
php56 -v
php7 -v
You can also run command refreshenv to reload environment variables in opened cmd.
If you want change default php version, you can move paths as below.
ex if you want php 7.0.23 as default instead of php 7.3.6
see screenshot for example
In above screenshot I have move php 7.0.23 above the all other php verions (php 7.3.6 and php 5.6.31).
I hope this will help.
Its quite simple really.
Create yourself a batch file to add php to the path, place that file in one of the folders that is currently on your PATH so you can run it from anywhere.
Then whenever you are in any folder that has CLI scripts in it run the batch file
Example batch file:
Lets call it PHPPATH.CMD
path=%path%;c:\wamp\bin\php\php5.4.11
php -v
Now if you want to use another version of the PHP CLI just change the batch file or if you are feeling clever make the batch file accept a paramter so you can specify the version of php you want on the path.
If you are able to do so, just create aliases:
alias 'php56'='/Path/To/php5.6/bin/php';
alias 'php55'='/Path/To/php5.5/bin/php';
If not, create links in a seperate directory that point to the binaries.
php56 is a link to /Path/To/php5.6/bin/php
php55 is a link to /Path/To/php5.5/bin/php
put these into /Seperate/Path/ and add '/Seperate/Path/' to %PATH%
Hope this helps
I've searched how to do it really easily without changing the environment variable path with the command line (because it's character limited) and find this solution (only on windows 10) :
in your environment variable path , change [D:\wamp64\bin\php\"your php version"] in [D:\wamp64\bin\php\php]
then create a junction between your php version directory and D:\wamp64\bin\php\php by running this command in cmd as administrator :
mklink /J D:\wamp64\bin\php\php D:\wamp64\bin\php\php"your php version"
then you can use this batch file
echo off
::read the actual php version
::(read the second word of php -v output )
for /f "tokens=2" %%i in ('php -v') do (
::if actual version is 7.4.0 I want 7.4.1
if "%%i"=="7.4.0" set phpV="7.4.1"
::if actual version is 7.4.1 I want 7.4.0
if "%%i"=="7.4.1" set phpV="7.4.0"
)
::delete the old junction
rmdir "D:\wamp64\bin\php\php"
::create a new one to the version I want
::!!!!!!!!!!!!!!!!!!! paths must not contains spaces
mklink /J D:\wamp64\bin\php\php D:\wamp64\bin\php\php%phpV%
now you can run your batch file in your cmd and don't even have to restart it after.

PHP: How can I tweak PHPUnit to use a different PHP interpreter?

My system has two PHP interpreters. One came bundled with the OS and the other I installed via the XAMPP package. All of my PHP extensions are applied to the XAMPP installation but PHPUnit seems to only run the version of PHP that came with my machine.
Does anybody know of a way I can configure or rebuild PHPUnit so that it uses my XAMPP PHP interpreter exclusively?
For Mac/Linux, the first line of the phpunit script with starts with
#!/usr/bin/php
change that to
#!/Applications/XAMPP/xamppfiles/bin/php
or whatever other php interpret you want to use.
Find the folder you installed PHPUnit in. There should be a file called phpunit.bat. It should have a line that reads something like
set PHPBIN="C:\php\php.exe"
%PHPBIN% "C:\php\phpunit" %*
Change it to read
set PHPBIN="C:\xampp\php\php.exe"
%PHPBIN% "C:\xampp\php\phpunit" %*
Or whatever the path to your PHP executable is
Since modifying phpunit file did not work for me because of phar signature errors, I was running phpunit with different php version by calling interpreter explicitly (on Linux):
php7.1 /usr/local/bin/phpunit
php5.6 /usr/local/bin/phpunit
Following the example with XAMPP, full path to php interpreter could be provided:
/Applications/XAMPP/xamppfiles/bin/php /usr/local/bin/phpunit
In agreement with Thomas' statement, additionally there's a line further below
if (strpos('/Applications/MAMP/bin/php5.3/bin/php', '#php_bin') === 0) {
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
}
That I've been told you're also supposed to change to reflect the PHP you're interested in using
(I've set mine to MAMP obviously)
I've switched back and forth from 5.2 and 5.3 a lot recently :)
This applies to phpunit installed using Homebrew on Mac OS 10.9. I’ve editing the file located at /usr/local/Cellar/phpunit/4.2.6/bin as seen below. CAVEAT: I don’t know how Homebrew will handle this on a PhpUnit update, but for now it’s working to be able to select the php version that PhpUnit is using for it's testing.
#!/usr/bin/env bash
php=/Applications/MAMP/bin/php/php5.3.14/bin/php
#php=/Applications/MAMP/bin/php/php5.4.4/bin/php
/usr/bin/env $php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/4.2.6/libexec/phpunit-4.2.6.phar $*
On Windows, this may be achieved using a similar approach to the ones mentioned in other replies.
In your /path/to/composer/phpunit directory, open the phpunit file in an editor. The first line should look like this:
#!/usr/bin/env php
Simply download your desired version of PHP for Windows, place the contents of the ZIP file somewhere to your liking, and reference the fully quantified path to the php.exe file, instead of just php. Like so:
#!/usr/bin/env /c/misc/php-5.5.9-nts-Win32-VC11-x86/php.exe
In my case, I put it in /c/misc/php-5.5.9-nts-Win32-VC11-x86/, which corresponds to C:\misc\php-5.5.9-nts-Win32-VC11-x86\ using Windows path syntax.
Remember to verify that the correct php.ini file is being used (php --ini or in a script file php_ini_loaded_file()).

Categories