Solved

echo a hello

Posted on 2009-05-12
2
278 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now