Jump to content
Larry Ullman's Book Forums

Recommended Posts

Good evening,


This book is my first foray into programming and I have a question regarding one of the tips in chapter 2.


"Unlike some other languages, PHP

generally doesn’t require you to declare or

initialize a variable prior to use. In other words,

you can refer to variables without first defining

them. But it’s best not to do that; I try to write

my scripts so that every variable is defined or

validated before use."


What do you mean by declare or initialize the variable before use?


Any help with this would be greatly appreciated.



  • Upvote 1
Link to post
Share on other sites

In some other languages, like Java, you have to declare a variable before you use it. What declaring them really does, it allocating a place in the memory for the information you save in a variable. It might be a bit technical, but you should not really care why it's like this. Just understand how to use variables.


This is Java with predeclared variables:


String firstName;
String lastName;
String adress;
Int phone;
Int age; 
Int credit_card_number = null;

// now we can use these variables below in the script.

firstname = "Thomas";
lastname = "Lastname";
String fullname = (firstname . ' ' . lastname);
// echo statement


PHP also is a loosly typed language, unlike Java. That means you don't have to set the type of information the variable should save (numbers, text, booleans, etc). It's done automaticly, like declaration is.


$firstname = "Thomas";
$lastname = "Lastname";
$fullname = $firstname . ' ' . $lastname;
echo $lastname

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...