Results 1 to 31 of 31

Thread: Length of a string

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    The United Kingdom
    Posts
    45

    Length of a string

    In Visual basic there is this function:-

    len(string)

    Which will return the number of characters in the given string.

    Is there a similar function in PHP?
    Please don't discourage me, I never hurt anyone.

    "You're watching FOX News channel, real journalism, fair and balanced." - Ha

  2. #2
    Lively Member
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    96
    strlen("$var")
    http://www.raketje.com coming..............soon................

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    The United Kingdom
    Posts
    45
    Thanks, I'll give it a try.
    Please don't discourage me, I never hurt anyone.

    "You're watching FOX News channel, real journalism, fair and balanced." - Ha

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    bekkel, what's the point of the quotes? They are not needed.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    The United Kingdom
    Posts
    45
    I tried it with and without the quotes and it worked both times. I don't know if it depends what version your using or anything. Im using PHP version 4.2.2. Speaking as a visual basic programmer it does seem odd to have to enclose variable names in quotes but then alot of things in PHP seem odd to me so i'll just shut up now.
    Please don't discourage me, I never hurt anyone.

    "You're watching FOX News channel, real journalism, fair and balanced." - Ha

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Yes, it should work with and without quotes in all versions, and I have seen various people using quoted variables in string functions...but I do not understand why?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    the only reason I can think of is if the variable isn't a string.....
    $myVar = 12.345

    strlen("$myVar");
    -- Just a guess though....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    That may be, but the casting happens automaticall:

    Code:
    <?php
        $int = 423;
        $double = 42.3;
        $string = "42.3";
    
        echo 'strlen($int) = ' . strlen($int) . '<br />';
        echo 'strlen($double) = ' . strlen($double) . '<br />';
        echo 'strlen($string) = ' . strlen($string) . '<br />';
    ?>
    It might give a notice, but I (and most people) have them turned off.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Lively Member
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    96
    I normaly don't use ("$var")
    but I've copied it form a source fo someone else..and did't check the for correct spelling (as it worked)
    http://www.raketje.com coming..............soon................

  10. #10
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    I agree with hobo. quotes are not needed in functions or whatever if it is a variable.

    PHP Code:
    $var "4.3";
    $var 4.3;  // might not work
    echo "$var";
    strlen("$var"); 
    that doesn't make any sense to me either.

    PHP Code:
    $var "4.3";
    $var2 4;
    echo 
    $var;
    strlen($var); 
    it shouldn't matter if is it a string or characters. the only time I believe it might matter is when you have more than a number. like above "4.3" will work but if you just had 4.3 it might not.

    if it is a variable then you don't need quotes in functions. saves on a couple bytes here and there and those can add up if you have a 100-1000 line file.
    Last edited by phpman; Sep 14th, 2003 at 02:07 PM.

  11. #11
    Lively Member morrowasted's Avatar
    Join Date
    Aug 2003
    Location
    Houston, TX
    Posts
    118
    Originally posted by MixMaster
    I tried it with and without the quotes and it worked both times. I don't know if it depends what version your using or anything. Im using PHP version 4.2.2. Speaking as a visual basic programmer it does seem odd to have to enclose variable names in quotes but then alot of things in PHP seem odd to me so i'll just shut up now.
    if you start out programming anything but VB, then learn VB, it will seem way odd.

    i mean... what other language uses "then", "and", and "or" in its If statements ???

    -morrowasted

  12. #12
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by morrowasted
    if you start out programming anything but VB, then learn VB, it will seem way odd.

    i mean... what other language uses "then", "and", and "or" in its If statements ???
    Gregory, php also uses "and" , "or" and instead of "then" it uses {

  13. #13
    Lively Member
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    96
    {
    saves a couple bytes here
    }
    http://www.raketje.com coming..............soon................

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    Gregory, php also uses "and" , "or" and instead of "then" it uses {
    I think he knows that phpman.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by The Hobo
    I think he knows that phpman.
    you would be surprised

  16. #16

    Thread Starter
    Member
    Join Date
    Sep 2003
    Location
    The United Kingdom
    Posts
    45
    I'm sorry. I did'nt want to start an argument just about some quotation marks.
    Please don't discourage me, I never hurt anyone.

    "You're watching FOX News channel, real journalism, fair and balanced." - Ha

  17. #17
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I think he was just talking about how VB is more like structured English than almost all other languages where we have cool funky things like && || != { }.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  18. #18
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by MixMaster
    I'm sorry. I did'nt want to start an argument just about some quotation marks.
    Who's arguing? We're just rambling like drunkards.

    It's what we do when no one is asking questions.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  19. #19
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    vb's so much more forgiving with its structure i think ;p

    in c, for example

    if (var = 6) {

    is one of the things that normally causes me problems

  20. #20
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    how is VB more forgiving?

    I think the only way VB is more forgiving is because it has a live error checker watching your every move. It also corrects your own syntax errors, which I hate, because I believe if you program, you should actually do the programming, and not a program. That's a reason why I despise FrontPage and DreamWeaver, and even VB itself.

    I open VB and try to do an if statement, and I always forget that stupid "then," and then whenever I try to do a while statement I always add the "then" and it tells me it's wrong.

    How would "if(var = 6){" get you? I don't know C at all; but did you just check the validility of 'var' to be 6, or did you define it?
    Like Archer? Check out some Sterling Archer quotes.

  21. #21
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    I think he is getting at is this.

    in C it is

    if (var = 6) {

    and in php it is

    if (var == 6) {


    he forgets the double equal sign and php will not run that code if you do.

  22. #22
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    uhh, actually PHP will interpret that fine.

    Code:
    <?
      if($var = 6){
        echo $var;
      }
    ?>
    You are just defining $var within the if statement. PHP will then take it as "if($var = true){" and as long as you don't define it as 0 or false, it will echo $var. The way you'd use that sometimes is have a variable and then define another variable as that variable to check if it's true or not..

    Like this:

    Code:
    <?
      $var1 = "blah";
      if($var2 = $var1){
        echo "var2 is true!";
        //$var1 is true, therefor $var2 is also true
        //it WILL echo
      }
      $var1 = 0;
      if($var2 = $var1){
        echo "var2 is true!";
        //$var1 is false, therefor $var2 is also false
        //it WILL NOT echo
      }
    ?>
    Don't know if that made any sense to anyone but myself..
    Last edited by kows; Sep 21st, 2003 at 02:38 PM.
    Like Archer? Check out some Sterling Archer quotes.

  23. #23
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    PHP Code:
    <?
      if($var = 6){
        echo $var;
      }
    ?>
    all you did there was set $var to equal 6. nothing more. it will always be true.

    you are not comparing the variables. I don't know of any situation that that will come in handy. if I wanted to do that then I just

    if($var){
    echo "true"
    }

    or

    if(!$var){
    echo "false"
    }

    your way isn't worth anything.

    if you want to compare then you must use ==

    http://us3.php.net/language.operators.comparison
    Last edited by phpman; Sep 21st, 2003 at 02:56 PM.

  24. #24
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    WOW! A contradiction all in one post!

    Originally posted by kows
    I think the only way VB is more forgiving is because it has a live error checker watching your every move. It also corrects your own syntax errors, which I hate, because I believe if you program, you should actually do the programming, and not a program. That's a reason why I despise FrontPage and DreamWeaver, and even VB itself.

    I open VB and try to do an if statement, and I always forget that stupid "then," and then whenever I try to do a while statement I always add the "then" and it tells me it's wrong.
    So, does VB correct your syntax errors or not? Some how I don't think it does. At any rate, if you forget the THEN off of an IF...THEN statement and adding it to a WHILE, that's not VB's fault. It's your lack of knowledge for not putting it in the first place. It's the same as if I forgot the opening bracket after an If in C(++) or PHP. If VB is "fixing" your syntax errors, you wouldn't have that problem. As for FP and DW, I'll grant you that.... all my HTML is done in notepad for that very reason.

    Originally posted by kows
    How would "if(var = 6){" get you? I don't know C at all; but did you just check the validility of 'var' to be 6, or did you define it?
    In "C" you just checked it, but in PHP, you just set it.....
    I work in VB, PHP and C#.... I don't have a problem switching back andf forth.....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  25. #25
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Originally posted by phpman

    all you did there was set $var to equal 6. nothing more. it will always be true.

    you are not comparing the variables. I don't know of any situation that that will come in handy. if I wanted to do that then I just

    your way isn't worth anything.

    if you want to compare then you must use ==

    http://us3.php.net/language.operators.comparison
    I don't really think you got what I meant; so I'm not going to try to explain it. If you read, I did say when you are doing "if($var = 6){" it is defining $var as 6, and that as long as you don't define it as 0 or false within the statement it would always be true. Really, you didn't get what I meant, and I was just proving you wrong anyway, which you then agreed that I did, so it's fine.

    he forgets the double equal sign and php will not run that code if you do.
    It does run it, it just won't do what you think it will.

    Tech, I just started VB about a week or two ago, so I can't say I'm that used to the language. I'm used to C based languages and not any MS made language like VB or ASP. I think they're stupid, but I'm taking a VB class and I can't just quit it right now. What I meant by syntax errors was things like it finishes your code for you, or it closes quotes, or correctly ends an if. Like, if you type in "endif" it will change it to "End If," and if you type in a objects name, like "Label1," and then try to change a property, it will bring up a menu to find the specific property you want. Then it'll bring up a menu to find what you want to change the property to. I guess, yeah, it isn't all that annoying to most people, but it is to me. I am mostly a web developer, so I'm used to just opening Notepad and typing a script fully by hand.
    Like Archer? Check out some Sterling Archer quotes.

  26. #26
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by da_silvy
    vb's so much more forgiving with its structure i think ;p

    in c, for example

    if (var = 6) {

    is one of the things that normally causes me problems
    if you don't use a double equals sign, it's assignment

    yet C won't alert you to this error

    also, variables aren't initialized in C

    it'll let u do some memory things that you shouldn't be able to as well

    that's off the top of my head

  27. #27
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by kows
    I don't really think you got what I meant; so I'm not going to try to explain it. If you read, I did say when you are doing "if($var = 6){" it is defining $var as 6, and that as long as you don't define it as 0 or false within the statement it would always be true. Really, you didn't get what I meant, and I was just proving you wrong anyway, which you then agreed that I did, so it's fine.



    It does run it, it just won't do what you think it will.

    yes I did understand you. your way is pointless as the only time you should use a if statement is if you are trying to compare something, and you were not. all you did was set a variable to equal 6.

    again, no need for that and a waste of coding.

    I never said it won't run it. I said there is no need to do it that way.

  28. #28
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Actually, you did say it wouldn't run it, and that's why I brang it up in the first place..

    Originally posted by phpman
    he forgets the double equal sign and php will not run that code if you do.
    Like Archer? Check out some Sterling Archer quotes.

  29. #29
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you are correct.

  30. #30
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by kows
    Tech, I just started VB about a week or two ago, so I can't say I'm that used to the language. I'm used to C based languages and not any MS made language like VB or ASP. I think they're stupid, but I'm taking a VB class and I can't just quit it right now. What I meant by syntax errors was things like it finishes your code for you, or it closes quotes, or correctly ends an if. Like, if you type in "endif" it will change it to "End If," and if you type in a objects name, like "Label1," and then try to change a property, it will bring up a menu to find the specific property you want. Then it'll bring up a menu to find what you want to change the property to. I guess, yeah, it isn't all that annoying to most people, but it is to me. I am mostly a web developer, so I'm used to just opening Notepad and typing a script fully by hand.
    That's called "Intellisense" and it's a "feature" of the IDE, not VB....
    IF it bothers you so, you can do VB using NotePad. There are ays of compiling it from the command line too. There's nothing that says you HAVE to use the IDE. It's a pain in the arse to do it in Notepad, but I can be done (in fact, I jsut did it the other day). As for the "fixing" thing... Hey, I like that.... as someone who has mild dislexya, having intellisense helps me to know when I've mistypes and object's name. There are also ways to turn it off too.... Not sure how, but there should be a way.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  31. #31
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    *whistles to himself*
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width