Solved

echo a hello

Posted on 2009-05-12
2
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

724 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