Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Can I Use Spaces / Apostrophes In Variables?

    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 ):

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

    Is there any way to use both the   AND the apostrophe in a variable name?
    Last edited by Phred; 10-23-2009 at 01:19 AM. Reason: Resolved...

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    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.

    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

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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  , 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.
    Last edited by tomws; 10-22-2009 at 05:51 PM. Reason: highlighting
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    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  , 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?

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Phred View Post
    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/languag...les.basics.php

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

    now it's better,

    best regards
    Last edited by oesxyl; 10-22-2009 at 07:14 PM.

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    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....

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Phred View Post
    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.

    Quote Originally Posted by Phred View Post
    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.
    Are you a Help Vampire?

  • #9
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    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.

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Are you a Help Vampire?

  • #11
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    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!

  • #12
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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'.
    OracleGuy

  • #13
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oracleguy View Post
    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...

  • #14
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Phred View Post
    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...
    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.
    Last edited by oracleguy; 10-22-2009 at 10:46 PM.
    OracleGuy

  • #15
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    http://www.php.net/manual/en/languag...les.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]*'


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •