 |
|
|
|
06-08-2005, 02:38 PM
|
php help
|
Posts: 674
Name: James
Location: KENT
|
i doing this test code and it not working i think i done somthink wrong in the print section near the each buton as in my editor all the txt is in black except the each so im guessing i got a colon or somethink in wrong place
<?php
#script 1.9 -numbers.php
//set the variables
$quantity=30;//buying 30
$price=119.95;//$119.95
$taxrate=.05;//5%
//calculate the total.
$total=$quanty * $price;
$total=$total +($total * $taxrate);//add tax
$total=number_format($total,2);//format the results
//print the results
echo @you are purchasing <b>',$quantity, '</b>widget(s) at a cost of <b>$',$price, '</b>
each. with tx, the total comes to <b>$', $total, '</b>.';
?>
|
|
|
|
06-08-2005, 03:57 PM
|
|
Posts: 2,536
Location: Western Maryland
|
You need to surround your echo with double quotes so PHP will know to resolve the variables within the double-quoted string:
PHP Code:
echo "You are purchasing <b>$quantity</b> widgets at a cost of <b> $ $price</b> each. With tax, the total comes to <b>$ $total.</b>";
__________________
—Kyrnt
|
|
|
|
06-08-2005, 03:57 PM
|
|
Posts: 174
Location: Nigeria/Lagos
|
what error did you get ??? try this
//print the results
echo "you are purchasing"; <br/>',$quantity, '<br/>widget(s) at a cost of <br/>$',$price, '<br/>
each "with tx, the total comes"; to <br/>$', $total, '<br/>.';
__________________
Life is just lyke a school where everybody goes to learn one or two thing. the more u school, the more u learn more about school..The more we live our lifes.. the more we learn more about life.
Please login or register to view this content. Registration is FREE
|
|
|
|
06-08-2005, 04:01 PM
|
|
Posts: 674
Name: James
Location: KENT
|
also i have sorted frsit problem next one
$total=$total +($total * $taxrate);//add tax
the code is not working out the total as in this what i get when i run script
You are purchasing 30 widget(s) at a cost of $119.95 each. With tax, the total comes to $0.00.
|
|
|
|
06-08-2005, 04:03 PM
|
|
Posts: 174
Location: Nigeria/Lagos
|
is ur print working now ??
__________________
Life is just lyke a school where everybody goes to learn one or two thing. the more u school, the more u learn more about school..The more we live our lifes.. the more we learn more about life.
Please login or register to view this content. Registration is FREE
|
|
|
|
06-08-2005, 04:08 PM
|
|
Posts: 2,536
Location: Western Maryland
|
That is because you are reassigning $total with your format_number call. Use a different variable there and print it there. Remember that number_format returns a STRING whereas before it was a double.
PHP Code:
$totalAmt = number_format( $total, 2 );
echo "You are purchasing <b>$quantity</b> widgets at a cost of <b> $ $price</b> each. With tax, the total comes to <b>$ $totalAmt.</b>";
__________________
—Kyrnt
|
|
|
|
06-08-2005, 05:37 PM
|
|
Posts: 674
Name: James
Location: KENT
|
ok cool wht the difference between variables and constants i mean a variable is $name and a constant is NAME but dont they do the same sort thing
|
|
|
|
06-08-2005, 05:49 PM
|
|
Posts: 2,536
Location: Western Maryland
|
Variables are meant to change. Say you are adding up 10 numbers and keep a running total. The total will change as you add -- thus $total would be appropriate.
Constants are not meant to change. An example might be PI. define( "PI", 3.14); The value of PI cannot be changed and can be referred to in such a manner.
__________________
—Kyrnt
|
|
|
|
06-08-2005, 05:51 PM
|
|
Posts: 674
Name: James
Location: KENT
|
ah i see so variables can be changed depening on the code
where constants will not change as they be a fixed value
|
|
|
|
06-09-2005, 11:38 AM
|
|
Posts: 674
Name: James
Location: KENT
|
so what sort of time would i use a constants as i dont really see the point in it i understand it a fixed value but surley this variable is the same as this constant
<?php
$date=8 th june 2005;
echo'today date is $date.';
?>
<?php
define =('DATE' ,'8 th june 2005');
?>
|
|
|
|
06-09-2005, 11:54 AM
|
|
Posts: 2,536
Location: Western Maryland
|
A defined constant is very useful, perhaps if you want to present repetitive text to a user. It can be defined in an include file then used in all your PHP scripts. Another example would be if you have a database application and you only want to present 50 records per page. Your scripts could use such a constant in multiple places. The security of a constant is that it is immutable -- it cannot be changed.
In your question earlier, you would want to surround $date's values in quotes, by the way. But the reason you would use a variable versus a constant is whether or not you would need the value to ever change.
Another example I can think of is that I always define my email address as a constant so that the contact form scripts can use that constant when sending the email to me after a visitor has fileld out a feedback form.
__________________
—Kyrnt
|
|
|
|
06-10-2005, 04:48 AM
|
|
Posts: 674
Name: James
Location: KENT
|
hi im just learing where to place the quotes and just had a question really the book say to type this
echo "you are purchasing <b>$quantity</b>widget(s) at a cost of <b>\$$price</b>
each. with tax, the total comes to <b>\$$total </b>.";
1 question i typed this code and it works fine
echo "you are purchasing <b>$quantity</b>widget(s) at a cost of <b>$$price</b>
each. with tax, the total comes to <b>$$total </b>.";
so what are the backslashes for what the point of them if the code works fine without them
|
|
|
|
06-10-2005, 06:23 AM
|
|
Posts: 2,536
Location: Western Maryland
|
The dollar sign in PHP indicates that the string immediately following it is a variable. $total is a variable, for example. The backslash communicates to the PHP processing engine that the next character should be taken literally, that is to say that it has absolutely no programmatic significance -- that it should be treated just as a character, just a dollar sign.
The reason that it may work well without it is because the processing engine does have some intelligence about figuring such things out. My guess is that it knows when two dollar signs appear together that the first one is a literal and the second one may be the beginning of a variable. However, don't rely on such things. You are safer and it is considered better programmatic style to use the escape characters where it is necessary and appropriate.
__________________
—Kyrnt
|
|
|
|
06-10-2005, 06:25 AM
|
|
Posts: 674
Name: James
Location: KENT
|
cool thanks im sur3e i have some more question soon thank for all your help kyrnt
|
|
|
|
06-11-2005, 01:26 PM
|
|
Posts: 674
Name: James
Location: KENT
|
hi learing about the $var=stripsslashes($var); commaned in php is this a good code to add all my scripts i build
also where would i use the $var = trim ($var); command what is this good i undersntad it to trim the white spaces but where
Last edited by millwalll; 06-11-2005 at 02:09 PM..
|
|
|
|
06-12-2005, 09:17 AM
|
|
Posts: 2,536
Location: Western Maryland
|
You may want to use trim() if you want to be sure that you have a String with no spaces at either end. This would be useful if you want to compare a user-input field to some value. For example, if you want to know whenever a user types "Chicken" into a particular form field, you want to trim the user's input before you perform the check, because any stray spaces at the end would throw off the comparison.
__________________
—Kyrnt
|
|
|
|
06-12-2005, 09:30 AM
|
|
Posts: 674
Name: James
Location: KENT
|
oh i see so if i want to look for chicken and they entered it as spacechickenspace then i woudl use the trim to get ride of the two spaces
|
|
|
|
06-16-2005, 05:34 AM
|
|
Posts: 674
Name: James
Location: KENT
|
need help with this code this bit of code the [] why is this after interests does this tell php this is the key or somethink why would i need to put this in there
<p><b>Interests:</b><input type="checkbox" name="interests[]" value="Music" />Music</p>
also what does this do ($_POST['name']); does this code say if the name variable is less than 0 i.e null the print the name variable and you forgot to enter your name where this is ($_POST['name']); is this wrong and is meant to be $name
if (srelen($_POST['name'])>0){
$name =stripsslashes
($_POST['name']);
} else {
$name="null";
echo '<p><b>You forgot to enter your name!</b></p>';
hi i need help with this im just total confussed with this code from the point a to b
<?php
# script 2.11 -handle_about.php
if (srelen($_POST['name'])>0){
$name =stripsslashes
($_POST['name']);
} else {
$name="null";
echo '<p><b>You forgot to enter your name!</b></p>';
}
if (isset($_POST['interests'])){ dont understand from this point A
$ints = NULL;
foreach($_POST['interests']as $key =>$value){
$ints .= "$value, ";
}
$ints = substr($ints, 0, -2);
$interests = true;
}else{
$interests =NULL;
echp '<p><b> you forgot to enter your interests!</b></p>'; to this point here B
}
if ($name && $interests){
echo "thank you, <b>$name</b>.
you entered your interests as:<br /><tt>$ints</tt></p>";
}
?>
|
|
|
|
06-16-2005, 07:55 AM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
$_POST['name'] refers to a post variable, ie something that has been submitted from a form. The form control (probably a text field) will have name="name" in the html source. You can use $name instead, but only if register globals is on, and it's generally a good idea not to.
The checkboxes with the name "interests[]" are named that way because it wants them treated as an array. This way instead of calling them interests1 interest2 interests3 etc and then having to hunt for variable names in your php, you name all the checkboxes interests[], and when you submit the form, all the checkboxes get submitted in one array called $interests, with only the elements set that were checked.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
|
|
|
|
06-16-2005, 08:19 AM
|
|
Posts: 674
Name: James
Location: KENT
|
ok so if i hade a list of hobbies i would do the code below and it would tell to treat them all as a $hobbies
<p><b>Interests:</b><input type="checkbox" name="hobbies[]" value="games" />games</p>
|
|
|
|
|
« Reply to php help
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|