Skip to content

Instantly share code, notes, and snippets.

@richardjortega
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardjortega/9397258 to your computer and use it in GitHub Desktop.
Save richardjortega/9397258 to your computer and use it in GitHub Desktop.
currency phpeople challenge
<?php
require('/Users/richardjortega/.composer/vendor/autoload.php');
echo "Enter the amount in US dollars: $";
$money = read_stdin();
echo "What currency do you want to convert to? (E)uros, (P)esos, (B)ritish Pounds: ";
$currency = strtoupper(read_stdin());
$converted = convert($money, $currency);
echo "$$money is equivalent to $converted euros\n";
function convert($money, $currency)
{
$conversion_dict = array(
"E" => 0.73,
"P" => 13,
"B" => 0.60
);
\Psy\Shell::debug(get_defined_vars()); // breakpoint with interactive shell!!
$converted_value = $money * $conversion_dict[$currency];
return $converted_value;
}
// our function to read from the command line
function read_stdin()
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
return $input; // return the text entered
}
?>
@richardjortega
Copy link
Author

Install:

$ brew update && brew install composer
$ composer global require psy/psysh
# If asked what version constraint to use just type `*`

Update Your PATH for global CLI use:

export PATH=~/.composer/vendor/bin:$PATH

Interactive Shell Use:

$ psysh

Debugger Use:

<?php
require('/Users/richardjortega/.composer/vendor/autoload.php'); 
# updated 'richardjortega' to your user name
# find path with `which psysh`

# some code is happening here
\Psy\Shell::debug(get_defined_vars());
# code stops line before with interactive shell!!!

@richardjortega
Copy link
Author

Example running the demo

14:16:01 richardjortega@~/Dropbox/Code/phpeople/challenge_02_13_2014 (master) : php currency.php
Enter the amount in US dollars: $111
What currency do you want to convert to? (E)uros, (P)esos, (B)ritish Pounds: e

Psy Shell v0.1.1 (PHP 5.5.9 — cli) by Justin Hileman
>>> $money
=> "111"
>>> exit
Exit:  Goodbye.
$111 is equivalent to 81.03 euros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment