Jump to content

Need some help


Fmdpa

Recommended Posts

I am reading a book (Learning PHP 5), and there are a couple things on which I am getting hung up. 1. Are "echo" and "print" redundant? Are they from different PHP eras, and one is not recommended anymore?2. Same type of problem with "include" and "require"...?3. If $_POST is a variable, would $_BANANA do the same thing? In the cases I've seen $_POST used, I can't ever find $_POST's value being set. Is it a different kind of variable?My biggest problem is with understanding the printf() function. Here's an example from the book.

$price = 5; $tax = 0.075printf('The dish costs $%.2f', $price * (1+$tax));

It does give an explanation, but I just not getting it. Another example:

$zip='6520';$month=2;$day=6;$year=2007;printf("ZIP is %05d and the date is %02d/%02d/%d", $zip, $month, $day, $year);

It looks like the format rules are filled respectively with the listed variables and/or operations. I understand the padding characters and that the % sign is mandatory, but what do the "5d", "2d" and "2f" mean?

Link to comment
Share on other sites

you should go over to php.net and search over there, you should be able to find everything you need there.$_POST and $_GET refer to the way information can be passed between pages. You can look that up here on w3schools too.

Link to comment
Share on other sites

I understand what $_POST and $_GET are. In one, (post) the info is passed invisibly through the HTTP header, but in the other (get) the info is transferred via the URL. But in learning PHP, I learned that anything that begins with a $ sign is a variable. That's where I am confused.

Link to comment
Share on other sites

what are you confused about? POST and GET are global variables. but you are also right in that you indicate a variable to be used in a script by putting a $ before it.

Link to comment
Share on other sites

I had never learned about predefined variables I just took a peek at the PHP manual, and it cleared that issue up. Why I was confused is because I thought all variables had to be assigned a value by the code writer.

Link to comment
Share on other sites

not necessarily, although it is good habit to at least define them and some would argue to initialize them. I prefer both.

Link to comment
Share on other sites

Are "echo" and "print" redundant?
More or less.
one is not recommended anymore?
They'll both be around for a while.
Same type of problem with "include" and "require"
Include and require behave differently when the file is not found.
If $_POST is a variable, would $_BANANA do the same thing?
What? $_POST doesn't "do" anything, it's a container for data.
In the cases I've seen $_POST used, I can't ever find $_POST's value being set.
The values that go inside $_POST, $_GET, $_COOKIE, $_SERVER, etc are all set by PHP. You can set them yourself also though, e.g.:$_POST['xxx'] = 'hi';
It does give an explanation, but I just not getting it.
This should explain the format string:http://www.php.net/manual/en/function.sprintf.php
Link to comment
Share on other sites

Note that echo and print return different values (nothing and 1 respectively).

Link to comment
Share on other sites

No - it's their return value. They return the same thing no matter what parameters you put it. Consider:

<?php	$a = print "print"; //prints "print"	echo $a; //prints "1"	$b = echo $a; //error?>

Did you have a look at their manual pages?

Link to comment
Share on other sites

Note that echo and print return different values (nothing and 1 respectively).
I think this is because echo is a language construct, just like include and require, while print is a funciton.
Link to comment
Share on other sites

Print is actually a construct as well, but it is unusual in that it returns a value.

print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...