Solved

echo a hello

Posted on 2009-05-12
2
281 Views
Last Modified: 2012-05-06
hello there,
how can I make a php script that I can execute from the shell.. something like

/usr/bin/php /home/usr/website/script.php bob smith

and the script will display
'hello bob smith'

how can I do that?
0
Comment
Question by:XK8ER
2 Comments
 
LVL 7

Accepted Solution

by:
Jonah11 earned 300 total points
ID: 24369781
if the file below is saved as script.php, you execute it as:

>php script.php bob smith

and it will echo "hello bob smith"

you can alter it to account for single names only, or names with more than 2 words, etc
<?php
 
print 'hello ' . $argv[1] . ' ' . $argv[2];
 
?>

Open in new window

0
 
LVL 2

Assisted Solution

by:crash36936
crash36936 earned 200 total points
ID: 24369905
Jonah beat me to it...

These pages may help
http://us.php.net/manual/en/reserved.variables.argc.php
http://us.php.net/manual/en/reserved.variables.argv.php

Note that to use $argc and $argv, `register_globals` must be on.  Using $_SERVER['argc'] and $_SERVER['argv'] should always work.

Also note that if you run the script `php script.php bob smith`, then you're passing 2 arguments and will have to `echo` them all.  if you run `php script.php "bob smith"`, then the whole name is counted as 1 argument
<?php
    //note that $_SERVER['argv'][0] is always the script name
    echo 'Hello ' . $_SERVER['argv'][1] . PHP_EOL;
?>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question