Variables are "containers" for storing information. Show
Creating (Declaring) PHP VariablesIn PHP, a variable starts with the After the execution of the statements above, the variable Note: When you assign a text value to a variable, put quotes around the value. Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it. Think of variables as containers for storing data. PHP VariablesA variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for PHP variables:
Remember that PHP variable names are case-sensitive! Output VariablesThe PHP The following example will show how to output text and a variable: The following example will produce the same output as the example above: Example<?php Try it Yourself » The following example will output the sum of two variables: Note: You will learn
more about the PHP is a Loosely Typed LanguageIn the example above, notice that we did not have to tell PHP which data type the variable is. PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error. In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch. You will learn more about This set of PHP Interview Questions and Answers focuses on “Basics – 2”. 1. Which is the right way of declaring a variable in PHP? a) Only ii) Answer: d 2. What will be the output of the following PHP code?
a) Error Answer: c 3. Which of the following PHP statements will output Hello World on the screen? i) echo ("Hello World"); ii) print ("Hello World"); iii) printf ("Hello World"); iv) sprintf ("Hello World"); a) i) and ii) Answer: b 4. What will be the output of the following PHP code?
a) a Answer: d 5. What will be the output of the following PHP code?
a) 1 Answer: c 6. What will be the output of the following PHP code?
a) Error Answer: c 7. Which of the below statements is equivalent to $add += $add? Answer: b 8. Which statement will output $x on the screen? Answer: a 9. What will be the output of the following PHP code?
a) 123 Answer: a 10. What will be the output of the following PHP code?
a) get Answer: d Sanfoundry Global Education & Learning Series – PHP Programming. To practice all areas of PHP for Interviews, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP. Next Steps:
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses. Which is the not the right way of declaring a variable in PHP?1. Which is the right way of declaring a variable in PHP? Explanation: A variable in PHP can not start with a number, also $this is mainly used to refer properties of a class so we can't use $this as a user define variable name.
Which is the right way of declaring in variable in PHP?In PHP, a variable is declared using a $ sign followed by the variable name. Here, some important points to know about variables: As PHP is a loosely typed language, so we do not need to declare the data types of the variables. It automatically analyzes the values and makes conversions to its correct datatype.
Which is the correct way to declare a variable?To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).
Which of the following is not a variable scope in PHP *?There are only two scopes available in PHP namely local and global scopes. When a variable is accessed outside its scope it will cause PHP error Undefined Variable.
|