/    Sign up×
Community /Pin to ProfileBookmark

getting Length of STRING VAR???

I am using PHP for my user registratiOn. While i was trying to Validate user informatIOn using server side I gOt sOMe prObLems. BeLOw is the piece Of cOde.

[code=php]function validate() {
$errors;
$errorMsg;
if ($_POST[‘txtFName’].length < 4) {
$errorMsg += “Firs name must contain atleast 4 charecter<br/>”;
$errors += 1;
}
else
$errors = 0;

echo “$errorMsg”;

}[/code]

I want tO get the Length of the string. What is the cOde fOr it. Is that cOrrect which I have used $var.length ??
It didnOt wOrk & nO errOr mesages are cOming.

How can I get the Length of an string/variabLe?????

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 26.2007 — [url=http://www.php.net/strlen]strlen[/url]()
Copy linkTweet thisAlerts:
@n_alexievMay 27.2007 — check this.
<i>
</i>&lt;?php
$variable = 'This is not a JAVA :)';

echo strlen($variable);
?&gt;


Really this is not JAVA. ?
Copy linkTweet thisAlerts:
@redhatlookMay 27.2007 — And I think you should use this


$errorMsg .= "Firs name must contain atleast 4 charecter<br/>";
[/QUOTE]


not this

$errorMsg += "Firs name must contain atleast 4 charecter<br/>";
[/QUOTE]
Copy linkTweet thisAlerts:
@konithomimoMay 28.2007 — It is obvious that you are trying to go from JS to PHP. Try your code like this:

function validate() {
$errors = 0;
$errorMsg = '';
if (strlen($_POST['txtFName']) &lt; 4){
$errorMsg .= "First name must contain atleast 4 charecter&lt;br/&gt;";
$errors .= 1;
}

echo $errorMsg;
}


A few notes:

  • - PHP uses a period in place of a plus sign when concatenating strings or adding numerical values to variables.

  • - You don't need quotation marks around variable names when adding them to strings, or echoing them.
  • Copy linkTweet thisAlerts:
    @firebladeauthorMay 31.2007 — I overcame this errOr. Thnx every one heLped me.
    ×

    Success!

    Help @fireblade spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...