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?
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.
Variable names are case sensitive. Notice how you spell $FirstName and $Firstname.
$Firstname <> $FirstName