...

View Full Version : Resolved Can I Use Spaces / Apostrophes In Variables?



Phred
10-22-2009, 04:09 PM
I'm using non-breaking spaces in my variables, and it works okay. The problem is that I want to receive the data with the words capitalized.
So if someone enters, "grand falls" it will be sent as "Grand Falls".

This is what I'm using and it doesn't work (obviously :p):

$Applicant's Town = ucwords("$Applicant's Town");

Is there any way to use both the   AND the apostrophe in a variable name?

tomws
10-22-2009, 04:28 PM
Why on earth would you use either of them? Besides making your code an unreadable, unwieldy, meaningless pile of characters, I mean. See also, self-documenting (http://en.wikipedia.org/wiki/Self-documenting).

Phred
10-22-2009, 04:35 PM
Why on earth would you use either of them? Besides making your code an unreadable, unwieldy, meaningless pile of characters, I mean. See also, self-documenting (http://en.wikipedia.org/wiki/Self-documenting).


To make it a bit easier to read and use in the database.

I want to get: Applicant's Town: Grand Falls

Rather than: Applicants_Town: Grand Falls

tomws
10-22-2009, 04:50 PM
Explain what you mean by "easier to read and use in the database". If you're referencing handling results from the db, is $Applicant's Town significantly easier to use and read than $row["Applicant's Town"]?

I'm not trying to give you a hard time, and I'm all for personality reflected in code, but there should be limits. When it comes to whitespace, apostrophes, and :eek:  , you're dealing with a fair handful of reserved tokens (in both PHP and MySQL) and opening up a good-sized possibility for error. Code maintenance could easily become more troublesome and less pleasurable than stabbing yourself in the groin with kitchen utensils.

Phred
10-22-2009, 05:13 PM
Explain what you mean by "easier to read and use in the database". If you're referencing handling results from the db, is $Applicant's Town significantly easier to use and read than $row["Applicant's Town"]?

I'm not trying to give you a hard time, and I'm all for personality reflected in code, but there should be limits. When it comes to whitespace, apostrophes, and :eek:  , you're dealing with a fair handful of reserved tokens (in both PHP and MySQL) and opening up a good-sized possibility for error. Code maintenance could easily become more troublesome and less pleasurable than stabbing yourself in the groin with kitchen utensils.



I have a form application on my web site where people input personal information (name, city, phone, etc.). And people sometimes enter the data in lowercase and it would be easier to read it if it was capitalized. IE, 'Grand Falls' vs. 'grand falls'.

If I use an underscore in the variable name, the code works fine. But then I have to manually remove the underscore from the received application before I can input the data. IE, change 'Applicants_Town' to 'Applicants Town'.

If it's not possible to use those characters in variable names, would it be possible to use an underscore in the variable name and then have the php script replace the underscore with a space? And what about keeping the apostrophe?

oesxyl
10-22-2009, 06:10 PM
I have a form application on my web site where people input personal information (name, city, phone, etc.). And people sometimes enter the data in lowercase and it would be easier to read it if it was capitalized. IE, 'Grand Falls' vs. 'grand falls'.

If I use an underscore in the variable name, the code works fine. But then I have to manually remove the underscore from the received application before I can input the data. IE, change 'Applicants_Town' to 'Applicants Town'.

If it's not possible to use those characters in variable names, would it be possible to use an underscore in the variable name and then have the php script replace the underscore with a space? And what about keeping the apostrophe?
this is a bad idea, :)
what about makeing a variable from 'rm -f /', :)

user input are, in my opinion, values for whatever variables you want but never variable names.
in a php variable you can use only chars from a-z, A-Z, _, first letter of the variable name and one or more a-z, A-Z, \x7f-\xff, :)

http://www.php.net/manual/en/language.variables.basics.php

oops, I forget something in my variable, must be: 'rm -rfv /'

now it's better, :)

best regards

Phred
10-22-2009, 06:25 PM
what about making a variable from 'rm -f /',

I have no idea what that means at all -- I'm not that familiar with PHP.

What will that do.... :confused:

tomws
10-22-2009, 07:04 PM
I have a form application on my web site where people input personal information (name, city, phone, etc.). And people sometimes enter the data in lowercase and it would be easier to read it if it was capitalized. IE, 'Grand Falls' vs. 'grand falls'.

That's common and easy with ucwords(). But that doesn't require weird variable names.


If I use an underscore in the variable name, the code works fine. But then I have to manually remove the underscore from the received application before I can input the data. IE, change 'Applicants_Town' to 'Applicants Town'.

If it's not possible to use those characters in variable names, would it be possible to use an underscore in the variable name and then have the php script replace the underscore with a space? And what about keeping the apostrophe?

Maybe you should show some code. I think you're trying to complicate a trivial task.

Phred
10-22-2009, 07:33 PM
Maybe you should show some code. I think you're trying to complicate a trivial task.

I posted my code in my first message. See above--^

All I really need is a way to make inputed data capitalized. I know how to do it with a normal variable name, I don't know how to do it with a variable name that contains a space.

tomws
10-22-2009, 07:41 PM
That's not "some code". It's a single line. I asked for code to determine why you're trying to do something weird like using reserved tokens in variable names and to see if we can point you in the right direction for how to handle your task appropriately.

Phred
10-22-2009, 08:17 PM
That's not "some code". It's a single line. I asked for code to determine why you're trying to do something weird like using reserved tokens in variable names and to see if we can point you in the right direction for how to handle your task appropriately.


I think you're right... the problem is trying to use reserved words in variables.
What I wanted to do was have a variable name with a space in it for easier reading, and I've done that and it works. Like this:

From the application form:
<input type="text" name="Applicant's&nbsp;City" size="20">

But if someone enters an answer in lowercase, then I want their input to show up capitalized. Actually it's really not all that important, I was more interested in knowing if it was possible to use reserved characters when you were processing the variables.

My structure works as-is, the problem is trying to make changes to the variable's content *before* its data is emailed to me.

I think I will leave it for now. Kinda fun to play around with tho! :thumbsup:

oracleguy
10-22-2009, 09:01 PM
I think you are some how under the impression that the variable name has some bearing on the data it contains. That isn't the case at all.

So using your input tag example from your last post, you wouldn't put &nbsp; in the name, if you want a space, put a space in it, that is why the attribute has quotes.

So if you had: <input type="text" name="Applicant's City" size="20">

And you wanted to make it proper case before you put it in the email, you would simply do:

$applicant_city = ucwords($_POST['Applicant\'s City']);

And then $applicant_city would have the proper case version of the text they entered. So if they entered 'great falls' on the form, $applicant_city would contain 'Great Falls'.

Phred
10-22-2009, 09:25 PM
I think you are some how under the impression that the variable name has some bearing on the data it contains. That isn't the case at all.

So using your input tag example from your last post, you wouldn't put &nbsp; in the name, if you want a space, put a space in it, that is why the attribute has quotes.

So if you had: <input type="text" name="Applicant's City" size="20">

And you wanted to make it proper case before you put it in the email, you would simply do:

$applicant_city = ucwords($_POST['Applicant\'s City']);

And then $applicant_city would have the proper case version of the text they entered. So if they entered 'great falls' on the form, $applicant_city would contain 'Great Falls'.


Thanx! That's a start.

If I leave out the space like you said and use <input type="text" name="Applicant's City" size="20">

the form sends it like this:

Applicant's_City: new york

Notice that it will add an underscore when it sends the data? That is why I added the &nbsp; to the code, like this:

<input type="text" name="Applicant's&nbsp;City" size="20">

Unfortunately (but not surprisingly) the non-breaking space is causing the code to crash. Gonna have to play with this some more... :D

oracleguy
10-22-2009, 09:44 PM
Thanx! That's a start.

If I leave out the space like you said and use <input type="text" name="Applicant's City" size="20">

the form sends it like this:

Applicant's_City: new york

Notice that it will add an underscore when it sends the data? That is why I added the &nbsp; to the code, like this:

<input type="text" name="Applicant's&nbsp;City" size="20">

Unfortunately (but not surprisingly) the non-breaking space is causing the code to crash. Gonna have to play with this some more... :D

Then don't use a space and frankly I would avoid using an apostrophe as well. Just call your input ApplicantsCity that will simplify the whole thing.

What do you mean it is causing it to crash, if with &nbsp in the name it should just be treated as a string. But regardless, my suggestion above seems like the best route. Because having a field called: Applicant's&nbsp;City is going only make things look weird and harder to read.

kbluhm
10-22-2009, 09:47 PM
http://www.php.net/manual/en/language.variables.basics.php


Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Phred
10-22-2009, 10:18 PM
Thanx guys for your input. I can see now that what I was trying to do is not permitted under PHP syntax,

$Applicants&nbsp;City = ucwords($Applicants&nbsp;City);

I wanted to make sure before I threw in the towel... :thumbsup: :D

oracleguy
10-22-2009, 11:00 PM
http://www.php.net/manual/en/language.variables.basics.php

I was referring to using that name as the HTML element name, not the PHP variable name since I assumed the OP wouldn't do that after repeated instructions that it is an invalid variable name.

Phred
10-22-2009, 11:20 PM
I was referring to using that name as the HTML element name, not the PHP variable name since I assumed the OP wouldn't do that after repeated instructions that it is an invalid variable name.

I suspected that it wasn't a valid variable name but wanted to see if there was some way around it.

You can close this thread... :)

oracleguy
10-22-2009, 11:38 PM
You can close this thread... :)

We don't do that here. However you can mark the thread as resolved. If you go and edit your first post (in the advanced view), there should be a drop down to change the thread prefix to resolved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum