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?
Printable View
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?
strlen("$var")
Thanks, I'll give it a try.
bekkel, what's the point of the quotes? They are not needed.
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.
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?
the only reason I can think of is if the variable isn't a string.....
$myVar = 12.345
strlen("$myVar");
-- Just a guess though....
That may be, but the casting happens automaticall:
It might give a notice, but I (and most people) have them turned off.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 />';
?>
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)
I agree with hobo. quotes are not needed in functions or whatever if it is a variable.
that doesn't make any sense to me either.PHP Code:$var = "4.3";
$var = 4.3; // might not work
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.PHP Code:$var = "4.3";
$var2 = 4;
echo $var;
strlen($var);
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.
if you start out programming anything but VB, then learn VB, it will seem way odd.Quote:
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.
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 {Quote:
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 :) ???
{
saves a couple bytes here :)
}
I think he knows that phpman. :pQuote:
Originally posted by phpman
Gregory, php also uses "and" , "or" and instead of "then" it uses {
you would be surprised :pQuote:
Originally posted by The Hobo
I think he knows that phpman. :p
I'm sorry. I did'nt want to start an argument just about some quotation marks.
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: ;)
Who's arguing? We're just rambling like drunkards.Quote:
Originally posted by MixMaster
I'm sorry. I did'nt want to start an argument just about some quotation marks.
It's what we do when no one is asking questions. :D
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
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?
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.
uhh, actually PHP will interpret that fine.
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..Code:<?
if($var = 6){
echo $var;
}
?>
Like this:
Don't know if that made any sense to anyone but myself..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
}
?>
all you did there was set $var to equal 6. nothing more. it will always be true.PHP Code:<?
if($var = 6){
echo $var;
}
?>
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
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.Quote:
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.
In "C" you just checked it, but in PHP, you just set it.....Quote:
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?
I work in VB, PHP and C#.... I don't have a problem switching back andf forth.....
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.Quote:
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
It does run it, it just won't do what you think it will.Quote:
he forgets the double equal sign and php will not run that code if you do.
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.
if you don't use a double equals sign, it's assignmentQuote:
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
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
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.Quote:
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.
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.
Actually, you did say it wouldn't run it, and that's why I brang it up in the first place..
Quote:
Originally posted by phpman
he forgets the double equal sign and php will not run that code if you do.
you are correct.
That's called "Intellisense" and it's a "feature" of the IDE, not VB....Quote:
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.
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.
*whistles to himself*