Solved

echo a hello

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now