Click to See Complete Forum and Search --> : Length of a string
MixMaster
Sep 12th, 2003, 03:18 AM
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?
bekkel
Sep 12th, 2003, 06:51 AM
strlen("$var")
MixMaster
Sep 12th, 2003, 07:55 AM
Thanks, I'll give it a try.
The Hobo
Sep 12th, 2003, 08:11 AM
bekkel, what's the point of the quotes? They are not needed.
MixMaster
Sep 12th, 2003, 08:53 AM
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.
The Hobo
Sep 12th, 2003, 11:45 AM
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?
techgnome
Sep 12th, 2003, 12:05 PM
the only reason I can think of is if the variable isn't a string.....
$myVar = 12.345
strlen("$myVar");
-- Just a guess though....
The Hobo
Sep 12th, 2003, 03:26 PM
That may be, but the casting happens automaticall:
<?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.
bekkel
Sep 14th, 2003, 11:08 AM
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)
phpman
Sep 14th, 2003, 02:03 PM
I agree with hobo. quotes are not needed in functions or whatever if it is a variable.
$var = "4.3";
$var = 4.3; // might not work
echo "$var";
strlen("$var");
that doesn't make any sense to me either.
$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.
morrowasted
Sep 14th, 2003, 02:31 PM
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 :) ???
phpman
Sep 15th, 2003, 01:07 AM
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 {
bekkel
Sep 15th, 2003, 02:34 AM
{
saves a couple bytes here :)
}
The Hobo
Sep 15th, 2003, 08:19 AM
Originally posted by phpman
Gregory, php also uses "and" , "or" and instead of "then" it uses {
I think he knows that phpman. :p
phpman
Sep 15th, 2003, 10:28 AM
Originally posted by The Hobo
I think he knows that phpman. :p
you would be surprised :p
MixMaster
Sep 15th, 2003, 11:28 AM
I'm sorry. I did'nt want to start an argument just about some quotation marks.
The Hobo
Sep 15th, 2003, 02:36 PM
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 && || != { }. :eek: ;)
The Hobo
Sep 15th, 2003, 02:37 PM
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. :D
da_silvy
Sep 20th, 2003, 09:33 PM
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 :p
kows
Sep 21st, 2003, 10:56 AM
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?
phpman
Sep 21st, 2003, 11:09 AM
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.
kows
Sep 21st, 2003, 02:33 PM
uhh, actually PHP will interpret that fine.
<?
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:
<?
$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..
phpman
Sep 21st, 2003, 02:52 PM
<?
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
techgnome
Sep 21st, 2003, 04:02 PM
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.....
kows
Sep 21st, 2003, 04:32 PM
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.
da_silvy
Sep 21st, 2003, 10:48 PM
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 :p
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
phpman
Sep 21st, 2003, 11:06 PM
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.
kows
Sep 22nd, 2003, 02:41 AM
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.
phpman
Sep 22nd, 2003, 08:24 AM
you are correct.
techgnome
Sep 22nd, 2003, 08:38 AM
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.
The Hobo
Sep 22nd, 2003, 10:28 PM
*whistles to himself*
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.