very basic question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

rleslie1970
Forum Newbie
Posts: 6
Joined: Thu Aug 16, 2007 5:45 pm

very basic question

Post by rleslie1970 »

I am new to PHP and am learning from a book. PHP - fast easy web development.

one of the exercises in the book has you build a very basic form to send out email. The form works fine.
I just have a question about the code that i don't quite understand.

$msg = "email sent from WWW site\n";
$msg .= "Sender's name:\t$_POST[sender_name]\n";
$msg .= "Message:\t$_POST[message]\n";


on the above message string, i understand everything except the "t". if i remove the T, then the email that gets returned, has $_POST[sender_name] etc. if i put the T in, it works fine. I understand that the 2 slash marks are escape characters, but what the heck is that T doing and why is it necessary.

any responses would be mucho appreciated.

:)
rleslie1970
Forum Newbie
Posts: 6
Joined: Thu Aug 16, 2007 5:45 pm

Post by rleslie1970 »

i mean the \t$_POST. that is the "t" i'm referring to.
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Post by Theory? »

It's the equivalent of a "Tab" in your string. Similar to how \n creates a newline in your output.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: very basic question

Post by superdezign »

rleslie1970 wrote:$msg .= "Sender's name:\t$_POST[sender_name]\n";
$msg .= "Message:\t$_POST[message]\n";
You should really be careful where you learn from. The fact that the book has failed to at least mention special characters is a bit of a giveaway. This code that you have shown would throw notices for using constants as strings. I really hope you didn't spend any money buying that book, because it looks to be giving you inaccurate information.

The correct way to write that post data into the code is:

Code: Select all

$msg .= "Sender's name:\t{$_POST['sender_name']}\n";
$msg .= "Message:\t{$_POST['message']}\n";
And by PHP standards, we wouldn't write "$msg .=" on every line... Just make the statement span more than one line.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Are you reading a book by Wrox Press?
rleslie1970
Forum Newbie
Posts: 6
Joined: Thu Aug 16, 2007 5:45 pm

Post by rleslie1970 »

Well, yes i did spend money on the book and it wasn't that expensive ($20). It's the PHP fast and easy web development by Julie Meloni. I tried learning from the hudzilla php website but didn't find it a very easy beginner website. This book has been pretty good at least explaining things from a novice's point of view. Obviously i'm not going to use it as my only point of reference.

A couple of questions....why is the "t" (tab) needed before the $_POST variable? and i noticed that when you posted the same code you put the {} brackets around the _$POST variable? Why?

If you have any resources that you could recommend for a beginner - books or websites, i would mucho appreciate it. What i like about the current book is at least they have you do some exercises in PHP. I can't learn if i'm just reading about PHP. I need to practice.

:)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

rleslie1970 wrote:A couple of questions....why is the "t" (tab) needed before the $_POST variable?
It isn't. It just creates a tab character. It's for the display.
rleslie1970 wrote:and i noticed that when you posted the same code you put the {} brackets around the _$POST variable? Why?
When you inline arrays, that's how you're supposed to do it.
rleslie1970 wrote:If you have any resources that you could recommend for a beginner - books or websites, i would mucho appreciate it. What i like about the current book is at least they have you do some exercises in PHP. I can't learn if i'm just reading about PHP. I need to practice.
None in particular... Just be wary of the one that you have.
jimminatel
Forum Newbie
Posts: 1
Joined: Fri Aug 17, 2007 7:06 pm

Post by jimminatel »

miro_igov wrote:Are you reading a book by Wrox Press?
Just to be clear, the book rleslie1970 mentions "PHP - fast easy web development" by Julie Meloni is NOT a Wrox book. It's a fairly old (2002) book from "Muska & Lipman/Premier." I have no opinion on its quality, I just know it isn't Wrox since I work for Wrox. Wrox's current PHP book for beginners is Beginning PHP 5." Creative, catchy title, I know. :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

and i noticed that when you posted the same code you put the {} brackets around the _$POST variable? Why?
It's well well worth reading this.
rleslie1970
Forum Newbie
Posts: 6
Joined: Thu Aug 16, 2007 5:45 pm

another question

Post by rleslie1970 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Actually, the book is from 2004.  

gotta another question. 

most of her examples work; however, on some of the forms that she does she puts this php code at the beginning so that if a user leaves any of the spaces blank, the form won't send.

Code: Select all

<?php
if (($_POST[sender_name] == "")  ||  ($_POST[sender_email]=="") || ($_POST[message] =="")) {
header("Location: simple_form.html");
}
So basically it should send you back to the original form/file. I should say that i'm creating a front end html form and using the post method to send it to a php file. However, when i put in the above code, i get an error message

"Cannot modify header information - headers already sent by (output started at /vservers/lm/htdocs/php_test/send_simpleform.php:2) in /vservers/lm/htdocs/php_test/send_simpleform.php on line 4".

I've tried placing the php code at different places in the file and that didn't help. The book is written for version 5 and the vps package i'm using (at work) is PHP 4.x something. Any advice would be appreciated. thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
dbrock178
Forum Newbie
Posts: 10
Joined: Sun Aug 19, 2007 1:08 am

Post by dbrock178 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Forgot the quotes.

Code: Select all

<?php
if (($_POST['sender_name'] == "")  ||  ($_POST['sender_email']=="") || ($_POST['message'] =="")) {
header("Location: simple_form.html");
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
rleslie1970
Forum Newbie
Posts: 6
Joined: Thu Aug 16, 2007 5:45 pm

Post by rleslie1970 »

thanks for the input regarding the syntax.


i was able to solve the problem though...there was a blank space before the php script.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yeah, this book is slowly losing my respect. :P

empty() is a better alternative to checking if data is equal to an empty string, as it checks if the variable is even set as well, and is useful for arrays.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Actually, I believe he's specifically reading PHP 5 - fast & easy web development, because that book is sitting less than 5 feet from my hands as I type this (and it's from 2004). It's a little unclear about some things, but overall, it is a VERY good book and has helped me through many projects (at least until I bothered to commit most of it to memory :D). It has a good reference for basic MySQL, simplexml, objects, and common core functions. So far, it's one of my best reference guides, second only to devnet. I agree with rleslie1970 in that it is a good book for learning by example, (even though I skipped most of it :D) but once you get PHP down, it is an excellent reference book as well.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This code that you have shown would throw notices for using constants as strings.
Actually in the context it is used in it will pass without issue according to the manual on strings. I personally wouldn't use that syntax, but it is valid PHP.

There are a few things going on here that I find somewhat appalling:

Code: Select all

<?php
$msg = "email sent from WWW site\n";
$msg .= "Sender's name:\t$_POST[sender_name]\n";
$msg .= "Message:\t$_POST[message]\n"; 
?>
There is no mention of validating input. Why in the world would a tutorial book, the kind used by beginners, not mention the fact that this is dangerous?

Insane concatenation when there is no need for it... Why not one string?

Including newlines and tabs in something so simple seems a little confusing as well. Personally I would have done something like:

Code: Select all

<?php
$msg = 'Email sent from WWW site
Sender\'s name:    ' . $_POST['sender_name'] . '
Message:    ' . $_POST['message'];
?>
Post Reply