AlexBeau Posted July 17, 2011 Share Posted July 17, 2011 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. Alex 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted July 17, 2011 Share Posted July 17, 2011 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 comment Share on other sites More sharing options...
Recommended Posts