tags:

views:

42

answers:

3

Here's my PHP code:

<html>
    <Head>
    <?php   $FirstName="Sergio";
                    $LastName="Tapia";
                    $firstNameCount=strlen($Firstname);
    ?>
    </Head>
    <body>
            <h1>It works!</h1>
            <?php echo("Why hello there Mr. ");
                      echo $FirstName . " " . $LastName;
                      echo "So, your name appears to have " . $firstNameCount . " letters." ?>
    </body>
</html>

It always shows that my firstNameCount is 0, when it should be six. Any ideas what I'm doing wrong?

Also, please tell me how to format code so it's in the recommended way. Since I'm literally brand new to PHP I'm probably not formatting as the standards say I should.

Thanks.

Edit: Also what's the difference between echo "texthere" and echo("text here"). I just noticed I'm using both (which I probably shouldn't be). Which should I use?

+5  A: 

PHP variable names are case-sensitive. Instead of $Firstname you should be referencing $FirstName:

$firstNameCount=strlen($FirstName);

As far as styling practices are concerned there is no language-wide standard. The best approach for formatting is to write code that is clean and readable. For your snippet above I would do it like this:

<?php
  $FirstName = "Sergio";
  $LastName = "Tapia";
  $firstNameCount = strlen($FirstName);
?>
<html>
  <head>
  </head>
  <body>
    <h1>It works!</h1>
    <?php echo "Why hello there Mr. " . $FirstName . " " . $LastName . "So, your name appears to have " . $firstNameCount . " letters." ?>
  </body>
</html>

The echo function can be used in either fashion (echo('') or echo ''), the only recommendation I would make is to pick one and stick with it.

Cryo
Your call to strlen is still using the wrong-cased version in your example code. (The 'n' isn't capitalized.)
clee
@clee Thanks for pointing that out. That example was for code styling, the bug fix was above that but I've updated my answer just in case.
Cryo
+1  A: 

Variable names are case sensitive. Notice how you spell $FirstName and $Firstname.

Vincent Ramdhanie
+1  A: 
$Firstname <> $FirstName
codingguy3000
In PHP, that would be '!='. As this '<>' does not equal this '!=' in PHP.
Mark Tomlin