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.

Slides:



Advertisements
Similar presentations
CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
Advertisements

Unit 3 Day 4 FOCS – Web Design. No Journal Entry.
HTML Minute University Richard Fisher 10/1/2001 HTML FSA Training2 HTML Overview  HTML  HyperText Markup Language.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
B.Sc. Multimedia ComputingMedia Technologies Database Technologies.
Creating WordPress Websites. Creating a site on your computer Local server Local WordPress installation Setting Up Dreamweaver.
Guide To UNIX Using Linux Third Edition
Web Programming Introduction to PHP COM Objectives To understand what PHP is and how a PHP script works with a Web Browser and a Web Server To learn.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
ITM352 Javascript and Dynamic Web Pages: Client Side Processing.
COMPUTERS AND INFORMATION SYSTEMS HTML. How the Web Works To access a web site  Enter its address (URL) in the address box of your browser 
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.
1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Python CGI programming
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
More on Variables Some related techniques. Header() function void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) header()
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Introduction to Web Programming. Introduction to PHP What is PHP? What is a PHP File? What is MySQL? Why PHP? Where to Start?
Introduction to JavaScript CS101 Introduction to Computing.
Introduction to Programming the WWW I CMSC Winter 2003.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
WAMP Windows Apache MySQL and PHP i.e. “WAMP”. Why WAMP? WampServer is a Windows web development environment. It allows you to create and test web pages.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
U:/msu/course/cse/103 Day 21, Slide 1 CSE 103 Makeups –If you didn’t take one over the weekend, take one TUESDAY or WEDNESDAY!
Unit 1 – Web Concepts Instructor: Brent Presley.
Chap 2 – Getting Started COMP YL Professor Mattos.
Creating PHP Pages Chapter 5 PHP Structure and Syntax.
Lesson 2 – Unit B. Quick review 1. What is the name of the eGCC host that you ftp your files? 2. What type of software do you use to create and edit web.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
PHP Form Processing * referenced from
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Linux Administration Working with the BASH Shell.
Radoslav Georgiev Telerik Corporation
PHP stands for …….. “PHP Hypertext Pre-processor” and is a server-side scripting language like ASP. PHP scripts are executed on the server PHP supports.
9/21/04 James Gallagher Server Installation and Testing: Hands-on ● Install the CGI server with the HDF and FreeForm handlers ● Link data so the server.
WampServer 2 Installation WAMP is a solution stack of open source programs used together to run dynamic Web sites or servers Most common expansion:  Windows,
Module 1 Introduction to JavaScript
HTML5 Basics.
CX Introduction to Web Programming
CSE 103 Day 20 Jo is out today; I’m Carl
Introduction to Dynamic Web Programming
Chapter 5 Scripting Language
Introduction and Principles
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
>> PHP: HTML Integration
Introduction to Programming the WWW I
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
Intro to PHP & Variables
IS333D: MULTI-TIER APPLICATION DEVELOPMENT
Simple PHP application
Testing REST IPA using POSTMAN
Web Systems Development (CSC-215)
Unit A.
PHP and Forms.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Intro to PHP.
Server Side Programming Overview And file system basics
Tutorial 7 – Integrating Access With the Web and With Other Programs
Introduction to PHP.
XAMPP.
Presentation transcript:

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

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

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

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>

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

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

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)

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"; ?>

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

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

<html> <head> <meta charset="utf-8"> <title>An Example</title> </head> <body> <p>The result is 6</p></body> </html>

PHP Manual, “Command Line PHP on Microsoft Windows” For more info, see PHP Manual, “Command Line PHP on Microsoft Windows” http://php.net/manual/en/install.windows.commandline.php PHP Manual, Features, “Using PHP from the command line” http://php.net/manual/en/features.commandline.php

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

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>

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 http://localhost/c322f12/test4.php 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 http://localhost/c322f12/test4.php?a=1&b[]=2&b[]=3

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

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

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

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

Array ( [girls] => Array [0] => Sue [1] => Pam ) [boys] => Array [0] => Al

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>

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) http://localhost/c322f12/array3?student[girls][]=Sue& student[boys]=Al&student[girls][]=Pam

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