In the command window, go to the folder containing test0.php E:\Some Folder>php test0.php The result is 5"> In the command window, go to the folder containing test0.php E:\Some Folder>php test0.php The result is 5">
Download presentation
Presentation is loading. Please wait.
Published byLewis Howard Modified over 6 years ago
1
PHP on the Command Line Often convenient to experiment with PHP in the command window, without involving the server Add the location of php.exe to your PATH environment variable In my installation, the location is C:\wamp\bin\php\php5.3.8 For Mac OS X, the location is /Applications/MAMP/bin/php5.3/bin Your path depends on Mac OS X and MAMP versions, so modify the following command accordingly In the Terminal, execute the following command: export PATH=/Applications/MAMP/bin/php5.3/bin:$PATH In the command window, can now execute a PHP file, say test0.php, directly E:\Some Folder> php test0.php
2
The following is test0.php
Example The following is test0.php <?php $a = 2; $b = 3; $c = $a + $b; echo "The result is $c"; ?> In the command window, go to the folder containing test0.php E:\Some Folder>php test0.php The result is 5
3
E.g., if the last code line in the above is changed to
Errors are reported E.g., if the last code line in the above is changed to $c = $a _ $b; we get (where I’ve wrapped long lines) E:\Some Folder>php test0.php PHP Parse error: syntax error, unexpected T_STRING in E:\Some Folder\test0.php on line 4 Parse error: syntax error, unexpected T_STRING in Redundant and parts are cryptic, but at least it gives the line number
4
E.g., if we replace the last code line in the above with
Of course, markup is not interpreted when a PHP file is executed via the command window E.g., if we replace the last code line in the above with echo "<p>The result is $c</p>"; the corresponding line of output becomes <p>The result is 5</p>
5
Put the following at the top of your PHP file
Can parse command line arguments into the $_GET variable using the parse_str() function Put the following at the top of your PHP file parse_str(implode('&', array_slice($argv, 1)), $_GET); Use $_GET, not $_POST, since only the GET method passes form data via the query string
6
Example The following is test1.php In the command window <?php
parse_str(implode('&', array_slice($argv, 1)), $_GET); $a = $_GET['a']; $b = $_GET['b']; echo "The value of a is $a\n"; echo "The value of b is [$b[0], $b[1]]\n"; $c = $a + $b[0] + $b[1]; // Implicit conversion of string to int echo "The result is $c"; ?> In the command window E:\Old D Drive\c322f12>php test.php a=1 b[]=2 b[]=3 The value of a is 1 The value of b is [2, 3] The result is 6
7
The PHP_SAPI constant contains the interface as a lower case string
PHP has a direct module interface called a SAPI (Server API) for different web servers as well as the command line The PHP_SAPI constant contains the interface as a lower case string Some of the many possible values are apache apache2filter cgi cli (the command line interface)
8
This lets us include command-line test code in PHP files normally executed by the server—e.g.,
if (PHP_SAPI === 'cli'){ parse_str(implode('&', array_slice($argv, 1)), $_GET); } $a = $_GET['a']; $b = $_GET['b']; echo "The value of a is $a\n"; echo "The value of b is [$b[0], $b[1]]\n"; $c = $a + $b[0] + $b[1]; // Implicit conversion of string to int echo "The result is $c"; ?>
9
If an entire HTML document is output, redirect output
E.g., test3.php is <?php if (PHP_SAPI === 'cli'){ parse_str(implode('&', array_slice($argv, 1)), $_GET); } $a = $_GET['a']; $b = $_GET['b']; ?> <html> <head> <meta charset="utf-8"> <title>An Example</title> </head> Continued
10
File test3.html is listed on the next slide
<body> <?php $c = $a + $b[0] + $b[1]; // Implicit conversion of string to int echo " <p>The result is $c</p>"; ?> </body> </html> At the command line E:\Some Folder>php test3.php a=1 b[]=2 b[]=3 > test3.html File test3.html is listed on the next slide
11
<html> <head> <meta charset="utf-8"> <title>An Example</title> </head> <body> <p>The result is 6</p></body> </html>
12
PHP Manual, “Command Line PHP on Microsoft Windows”
For more info, see PHP Manual, “Command Line PHP on Microsoft Windows” PHP Manual, Features, “Using PHP from the command line”
13
Testing by Including the Query String in the Address Bar
Can test via the server a PHP script that provides a response to a GET request Provide values for the form variables in the query string in the address bar
14
Example <?php if (PHP_SAPI === 'cli'){
parse_str(implode('&', array_slice($argv, 1)), $_GET); } $a = $_GET['a']; $b = $_GET['b']; ?> <html> <head> <meta charset="utf-8"> <title>An Example</title> </head> <body> $c = $a + $b[0] + $b[1]; // Implicit conversion of string to int echo " <p>The result is $c</p>"; </body> </html>
15
This is file test4.php in the folder C:\wamp\www\c322f12
Recall that C:\wamp\www\ is the document root To access this file via the server, put in the address bar To provide values for scalar a and array b, include a query string indicating a=1, b[]=2, b[]=3 Separate these associations with ‘&’s Separate the query string from the rest of the URL by a ‘?’ So the entire contents of the address bar are
16
Addition: Using Notepad++ as a Scratchpad for PHP
Easily test PHP code not expecting form data New Notepad++ file with PHP selected for Language Put opening <?php in 1st line, closing ?> in a later line Copy code to test between these brackets Include echo commands to see results Remove HTML markup, add \t and \n as needed Save and execute in the command line
17
Execute it in the command line
Example The file ar1.php, at right Execute it in the command line E:\SomeFolder>php ar1.php 1 cat 2 Dog To test other code, select the code just executed Type Ctrl + k
18
This comments out each line in the selected code
Enter the new code to test (uncommented) Save and execute again in the command line To uncomment code, select it, type Ctrl + q Toggle code between commented and uncommented as needed
19
Addition: 2D Arrays from the Command Line
Suppose cmd.php contains the following <?php parse_str(implode('&', array_slice($argv, 1)), $_GET); $ar = $_GET['student']; print_r($ar); ?> Execute it with (where I’ve split the single line) E:\SomeFolder> php ar1.php student[girls][]=Sue student[boys][]=Al student[girls][]=Pam White space only between (not within) command line arguments Strings not quoted Output on next slide
20
Array ( [girls] => Array [0] => Sue [1] => Pam ) [boys] => Array [0] => Al
21
Addition: Including the Query String in the Address Bar: 2D Arrays
The following is array3.php in folder c322f12 just below my document root <?php $ar = $_GET['student']; ?> <html> <head></head> <body> <pre> print_r($ar); </pre> </body> </html>
22
Make a request, passing a query string that encodes a 2D array student
What I typed in the address bar is (where I’ve split it over 2 lines) student[boys]=Al&student[girls][]=Pam
23
Tolerates whitespace on either side of ‘&’and ‘
Tolerates whitespace on either side of ‘&’and ‘?’, inside ‘[]’, and just left of ‘=’ Whitespace just right of ‘=’ and inside, e.g., ‘[girls]’ is incorporated into the string
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.