dukeu03 Posted September 17, 2008 Share Posted September 17, 2008 Hi, this is my first php assignment so please be patient (this is also one of my first time programming in general). My assignment is to creat a vending machine that has candybars for .65 and handles only .25, .10, and .05 values of currency. We are to use rand(0,10) to create a random number for each amount of currency and I believe this is where my issue lies. The code is not displaying, so I know I have an error, but also I am unsure if I am calling on rand properly. Please if anyone can guide me it would be much appreciated. <html> <head> <title>Vending Machine</title> </head> <body> <?php //define the amount of nickels $NR = (rand(0,10)); // define variable for nickels $N = .05; // define variable for total value of nickels $NT = $N*$NR; echo "Total Nickels is $NR <br />"; //define the amount of dimes $DR = (rand(0,10)); // define variable for dimes $D = .10; // define variable for total value of dimes $DT = $D*$DR; echo "Total Dimes is $DR <br />"; //define the amount of quarters $QR = (rand(0,10)); // define variable for quarters $Q = .25; // define variable for total value of quarters $QT = $Q*$QR; echo "Total Quarters is $QR <br />"; //define the total amount of money $MT = $NT+$DT+$QT; //use \$ to insert a $ echo "Total money is \$ $MT <br />"; //define the cost of a candy bar $CB = .65; //define total candy bars purchased $CT = $MT/$CB; echo "Amount of candy bars dispensed is $CT <br />"; //define the variable that illustrates change due using modulo $CHANGE = $MT%$CB; echo "Amount of change due is $CHANGE"; //find change by giving fewest coins possible, start by dividing change by quarters $CH1 = $CHANGE/$Q; echo "The amount of quarters is $CH1 <br />"; //finds change left after dispensing quarters and issues appropriate amount of dimes $CH2 = (($CHANGE%$Q)/$D); echo "The amount of dimes is $CH2 <br />"; ?> </body> </html> thanks for any help in advance Quote Link to comment https://forums.phpfreaks.com/topic/124658-first-php-assignment-page-not-displaying/ Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 I don't see any problem with this code. I just copied it onto my server and it works fine. Good job for you first PHP. Quote Link to comment https://forums.phpfreaks.com/topic/124658-first-php-assignment-page-not-displaying/#findComment-643844 Share on other sites More sharing options...
dukeu03 Posted September 17, 2008 Author Share Posted September 17, 2008 hmm, that sounds good to me, thanks for the help! ill contact my university to see why php is not working for my account. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/124658-first-php-assignment-page-not-displaying/#findComment-643854 Share on other sites More sharing options...
thesaleboat Posted September 17, 2008 Share Posted September 17, 2008 Try changing the double quotes to single quotes on the echo's " to -> ' Quote Link to comment https://forums.phpfreaks.com/topic/124658-first-php-assignment-page-not-displaying/#findComment-643886 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 I you do that, you won't get the values of the variables, but rather the names of the variables. In other words: echo 'Total Nickels is $NR <br />'; would literally print this: Total Nickels is $NR <br /> To use single quotes, you'd have to do this: echo 'Total Nickels is '.$NR.' <br />'; Quote Link to comment https://forums.phpfreaks.com/topic/124658-first-php-assignment-page-not-displaying/#findComment-643891 Share on other sites More sharing options...
thesaleboat Posted September 17, 2008 Share Posted September 17, 2008 Yes, sorry about that thanks for pointing that out. echo '<font color="red">'.$errors[$i] .'</font><br/>'; Quote Link to comment https://forums.phpfreaks.com/topic/124658-first-php-assignment-page-not-displaying/#findComment-643904 Share on other sites More sharing options...
wildteen88 Posted September 17, 2008 Share Posted September 17, 2008 Hi, this is my first php assignment so please be patient (this is also one of my first time programming in general). My assignment is to creat a vending machine that has candybars for .65 and handles only .25, .10, and .05 values of currency. We are to use rand(0,10) to create a random number for each amount of currency and I believe this is where my issue lies. The code is not displaying, so I know I have an error, but also I am unsure if I am calling on rand properly. Please if anyone can guide me it would be much appreciated. Can you tell us how you're running the code? Make sure you're not running it directly in your web browser (via Open With.. or File Open etc), as web browsers do not understand PHP. You need to run your code in a PHP enabled server environment, your university should provide information about this to you. Also most importantly make sure all PHP code is saved in a file ending in a .php file extension. Quote Link to comment https://forums.phpfreaks.com/topic/124658-first-php-assignment-page-not-displaying/#findComment-643949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.