Command line script in windows - php

I am trying to run a php script from the command line in windows using the following variations:
php script.php
php -f script.php
Both commands just print out the entire script but don't execute the script.

Sounds like you forgot to start the script with <?php.
Content in a PHP script outside of a <?php block goes direct to STDOUT.

Without contents of the script its difficult to see the issues.
Most likely is that you have not used <?php ?> tags or used the short hand. Try using the full <?php ?> tags.

Related

Running PHP Script In Powershell keeps opening NotePad ++ rather than executing the PHP script

Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php

I use php exec another php but this not working

i have this file:
pippo.php
exec(ExportExcel.php);
ExportExcel.php
#!/usr/local/bin/php
<?php
$stop=1;
......
php code
......
?>
The exec command runs the file but I don't know what it is doing. It remains hung up and I don't get to activate the breakpoint placed in the file.
Any idea?
Thnak's
You need to pass a string to exec(), so maybe try exec('./ExportExcel.php');
Reference: https://www.php.net/manual/en/function.exec.php

Find out what PHP is running on

I have a script which is running on Apache and also can be executed from command line.
How can i know, on what is php script running, apache or command line?
There is a constant build in PHP that you can use PHP_SAPI. If you are on the commandline the value of this constant is cli than you are on the command line. Every other value like cgi, cgi-fcgi, etc.
Why not add a parameter and only pass it when invoking from the command line?
http://php.net/manual/de/reserved.variables.argv.php
Create a page upload it and browse to it.
See page contents below.
<?php phpinfo(); ?>

Execute php file from another php

I want to call a PHP file that starts like
<?php
function connection () {
//Statements
}
I call from the PHP like this:
<?php
exec ('/opt/lampp/htdocs/stuff/name.php');
?>
I get:
line1-> cannot open ?: No such file
line 3 //Connection: not found
line 4 Syntax errror: "("
Why doesn't this correctly execute the name.php file?
It's trying to run it as a shell script, which interprets your <?php token as bash, which is a syntax error. Just use include() or one of its friends:
For example, in a.php put:
<?php
print "one";
include 'b.php';
print "three";
?>
In b.php put:
<?php
print "two";
?>
Prints:
eric#dev ~ $ php a.php
onetwothree
exec is shelling to the operating system, and unless the OS has some special way of knowing how to execute a file, then it's going to default to treating it as a shell script or similar.
In this case, it has no idea how to run your php file.
If this script absolutely has to be executed from a shell, then either execute php passing the filename as a parameter, e.g
exec ('/usr/local/bin/php -f /opt/lampp/htdocs/.../name.php)') ;
or use the punct at the top of your php script
#!/usr/local/bin/php
<?php ... ?>
Sounds like you're trying to execute the PHP code directly in your shell. Your shell doesn't speak PHP, so it interprets your PHP code as though it's in your shell's native language, as though you had literally run <?php at the command line.
Shell scripts usually start with a "shebang" line that tells the shell what program to use to interpret the file. Begin your file like this:
#!/usr/bin/env php
<?php
//Connection
function connection () {
Besides that, the string you're passing to exec doesn't make any sense. It starts with a slash all by itself, it uses too many periods in the path, and it has a stray right parenthesis.
Copy the contents of the command string and paste them at your command line. If it doesn't run there, then exec probably won't be able to run it, either.
Another option is to change the command you execute. Instead of running the script directly, run php and pass your script as an argument. Then you shouldn't need the shebang line.
exec('php name.php');
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
This runs as if you run the script from browser.

Why Doesnt PHP Parse My Scripts?

I'm trying to get the php executable to parse scripts, but it's not working. i run something like this:
php c:\test.php
and test.php contains this:
<?
echo 'hello!';
?>
and that is exactly what running the command returns. It returns the raw code. How can I get it to actually parse the code and return "hello!" instead?
Make sure you have the short_open_tag setting enabled in your php.ini. Failing that, use the ever-so-slightly longer <?php to start your script.
Another option might be to try using the -f command line option.
php -f c:\test.php
It's been a while since I've ran php off the command line, but try this:
<?php
echo 'hello!';
?>
Also make sure that your webserver is correctly configured. If it is not configured to run .php pages through the php interpreter it may simply display as a text file.

Categories