Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Explain a line of code please
Old 02-06-2010, 11:51 AM Explain a line of code please
fog
fog's Avatar
Experienced Talker

Posts: 47
Location: Florida
Trades: 0
I was given a short script to insert the results of a form into a db. The person who sent me the script is well versed in php, I am just trying to learn it.

Here is the line:

$result = mysql_query("insert into signup(email,zip) values('" . $email . "','NA')");

The reason that NA is in the zip field is that the form only asks for the email not the zip but the db requires a value.

My question is what is the purpose of the . in . $email .

Why could that part of the line be: values("$email",'NA')

I have searched the net and looked in two books for an answer and have not been able to find an explanation. Thanks for any help.
__________________
Every artist was first an amateur.
Ralph Waldo Emerson
fog is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-06-2010, 11:55 AM Re: Explain a line of code please
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
the "." character is the string concatenation symbol in PHP

http://php.net/manual/en/language.operators.string.php
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2010, 12:07 PM Re: Explain a line of code please
fog
fog's Avatar
Experienced Talker

Posts: 47
Location: Florida
Trades: 0
I know that. What is being concanted here? Again why could the line not be written as I asked above?
__________________
Every artist was first an amateur.
Ralph Waldo Emerson
fog is offline
Reply With Quote
View Public Profile
 
Old 02-06-2010, 12:11 PM Re: Explain a line of code please
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Because the variable, being a string needs single quote marks around it.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2010, 12:15 PM Re: Explain a line of code please
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Sorry I see what you "don't get"


IF the variable was just added to the string without the concatenation it would be seen a "literal", rather than "the value of" being inserted.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2010, 12:17 PM Re: Explain a line of code please
fog
fog's Avatar
Experienced Talker

Posts: 47
Location: Florida
Trades: 0
Chris,

You might think I am really slow, I am just trying to learn this stuff.

Why not write values('$email','NA') instead?

I am not being argumentative, I want to know the proper way to do this.

Thanks
__________________
Every artist was first an amateur.
Ralph Waldo Emerson
fog is offline
Reply With Quote
View Public Profile
 
Old 02-06-2010, 12:25 PM Re: Explain a line of code please
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Why not write values('$email','NA') instead?
No because that would become a string literal of "$email" NOT

'name@email.tld'
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2010, 12:47 PM Re: Explain a line of code please
fog
fog's Avatar
Experienced Talker

Posts: 47
Location: Florida
Trades: 0
Ok I think I am starting to understand but one more question, why write the line as above using the variable $result?

does the line:
mysql_query("insert into signup(email,zip) values("$email",'NA')");

not do the same thing and insert the data into the db?
__________________
Every artist was first an amateur.
Ralph Waldo Emerson

Last edited by fog; 02-06-2010 at 01:37 PM..
fog is offline
Reply With Quote
View Public Profile
 
Old 02-06-2010, 02:36 PM Re: Explain a line of code please
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
not do the same thing and insert the data into the db?
Nope.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2010, 02:44 PM Re: Explain a line of code please
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Originally Posted by chrishirst View Post
No because that would become a string literal of "$email" NOT

'name@email.tld'
I have to jump in the wagon here.
This is a PHP quircks (hu, no specialities).

If you use single quotes when you define a string, the $variable is not replaced.
It is only if you use the double quotes:
PHP Code:
$animal="fox";
$string1="the $animal jumps over the lazy dog";
//$string1 is now:
//the fox jumps over the lazy dog
$string2='the $animal jumps over the lazy dog';
//but $string 2 is now:
//the $animal jumps over the lazy dog 
But, you can have single quotes in a string, and don't need to concatenate the variables if the "outer" delimiter of the string are double quotes.
In the case of sql queries, this is sensible
PHP Code:
//this is valid, and $email will be replaced by the content of the variable
$query="insert into signup(email,zip) values('$email','NA')";
//here, using the single quotes delimiter forces us to concatenate the variable to the string AND escape the query single quotes
$query='insert into signup(email,zip) values(\''.$email.'\',\'NA\')'
And if you must use an array indice in your string, then you must enclose it's declaration between { and }
PHP Code:
$animals['dog']='woolfy';
$query="insert into animals (name) values ('{$animals['dog']}');"
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-06-2010, 04:37 PM Re: Explain a line of code please
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Ahh nooo

Ignore my post, I was being silly.

I see the outermost are ' not " so yeah you would have to '.$var.' to do it.

Facepalm at my stupidity.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 02-06-2010 at 04:40 PM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 02-06-2010, 04:59 PM Re: Explain a line of code please
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
If you talk about your first post, no, it's " that delimite the string, not '
PHP Code:
$result mysql_query("insert into signup(email,zip) values('" $email .  "','NA')"); 
and this could be written
PHP Code:
$result mysql_query("insert into signup(email,zip) values('$email','NA')"); 
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-07-2010, 11:44 AM Re: Explain a line of code please
fog
fog's Avatar
Experienced Talker

Posts: 47
Location: Florida
Trades: 0
Tripy,

Thank you for taking the time to explain this to me. You have been very helpful.

Chris may know what he is doing but sure seems uninterested in helping a new person learn. An answer like NOPE sure does me no good at all.
__________________
Every artist was first an amateur.
Ralph Waldo Emerson
fog is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Explain a line of code please
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41602 seconds with 12 queries