php


How to use the php command line interactively?


Are there any howtos for using the php command line interactively? I found a lot about running sripts that are in text-files, but not really about the shell with the prompt where I type in commands:
$ php -a
Interactive shell
php > echo "hello world";
hello world
php > $a = 1;
php > echo $a;
1
php > exit;
$
When I go to the linux shell and run php -a I get the php shell. Can I load classes that live in files? What are the rules here?
The rules aren't any different to a normal PHP script - just think of it like reading from a very slow disk... The only real difference is that it can't read ahead, so you have to define functions before you use them.
You can use include or require as normal to load classes.
Instructions to install phpsh in Ubuntu 10.04 Server edition.
Get phpsh source and extract
wget http://github.com/facebook/phpsh/zipball/master
sudo apt-get install unzip
mkdir temp
mv facebook-phpsh-8438f3f.zip temp
cd temp
unzip facebook-phpsh-8438f3f.zip
phpsh uses python, install dependencies
sudo apt-get install python-setuptools
sudo apt-get install linux-headers-$(uname -r)
sudo apt-get install build-essential
sudo apt-get install python-dev
sudo apt-get install sqlite3 libsqlite3-dev
sudo easy_install pysqlite
sudo apt-get install libncurses5-dev
sudo easy_install readline
Setup phpsh, run and see that is works
sudo python setup.py install
phpsh
$a = array("a"=>1,"b"=>2);
print_r($a)
The interactive mode for php is somewhat limited. You may find phpsh more useful.
I believe you can use include. You can include files relative to the location you called the command.
There is another minor difference that could be problematic if you rely on the class-autoloading-behavior of PHP:
Note: Autoloading is not available if
using PHP in CLI interactive mode.
Source: Using PHP from the command line and Autoloading Objects
Use Boris
Python has one. Ruby has one. Clojure has one. Now PHP has one too.
Boris is PHP's missing REPL (read-eval-print loop), allowing
developers to experiment with PHP code in the terminal in an
interactive manner. If you make a mistake, it doesn't matter, Boris
will report the error and stand to attention for further input.

Related Links

How to create mysql table in php using class(oops) in php?
codeigniter and DataMapper. The relationship table in different servers, how?
PHP: setlocale() does not work
PHP shell_exec 'wkhtmltopdf' returns null even though successful completion
change wordpress plugin source code; no change on the site
Display only the last 300 MYSQL Results with a PHP pagination system
Display modified date outside the loop
PHP does not store strings in $_SESSION
MongoDB - Aggregation Framework, PHP and averages
PHP if/else statement function problems
Linking out to URL directly from PHP search query
Foreach loop, and array building
Mysql WHERE NOT EXISTS (query) OR (query)
Select One Active Image from multiple Multiple Images
MySQL GROUP By and ORDER BY conflict
Syntax error in update trouble [closed]

Categories

HOME
tomcat
apple-push-notifications
postgresql-9.4
initialization
ontology
implicit-conversion
turbojpeg
navigation
stored-procedures
firebase-database
anchor
eddystone
log4j2
xades4j
android-externalstorage
jquery-file-upload
mailgun
texas-instruments
sonicwall
android-contacts
google-sites
renderscript
multiple-tables
spyder
liferay-6.2
connection-refused
rxjs5
tortoisemerge
high-availability
webviewclient
fstar
basex
cube
autodesk-model-derivative
plantuml
appcode
om-next
bean-validation
jfxtras
netstat
djcelery
cloudera-quickstart-vm
python-jira
coverage.py
fluid-mac-app-engine
spooler
jshint
swift2.3
mura
patternlab.io
emf-compare
adobe-reader
petsc
yui3
pyaudio
svnserve
perforce-integrate
apple-configurator
gravatar
openmrs
voronoi
flask-security
grinder
python-cffi
openshift-cartridge
cfwheels
gluon-desktop
uitest
vdm-sl
roxygen2
compositetype
modern.ie
bootstrap-wysiwyg
algebraixlib
componentart
ipod
flurry-analytics
device-admin
object-code
nodeload
wso2bam
typesafe-stack
proxy-server
pbkdf2
xsd2code
application-server
insertonsubmit
smartfox
first-class
java.lang.class
wiktionary
sigar
litespeed
sql-parametrized-query
asp.net-mvc-views
webresponse
hungarian-notation
outlook-2007-addin
fgetc
gtktextview
downcasting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App