Which is not the right way of declaring a variable in php


Variables are "containers" for storing information.


Creating (Declaring) PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable:

After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.

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 Variables

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)

Remember that PHP variable names are case-sensitive!



Output Variables

The PHP echo statement is often used to output data to the screen.

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
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>

Try it Yourself »

The following example will output the sum of two variables:

Note: You will learn more about the echo statement and how to output data to the screen in the next chapter.


PHP is a Loosely Typed Language

In 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 strict and non-strict requirements, and data type declarations in the PHP Functions chapter.




This set of PHP Interview Questions and Answers focuses on “Basics – 2”.

1. Which is the right way of declaring a variable in PHP?

i) $3hello
ii) $_hello
iii) $this
iv) $This

a) Only ii)
b) Only iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer

Answer: d
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.

2. What will be the output of the following PHP code?

  1.     <?php 
  2.     $foo = 'Bob';              
  3.     $bar = &$foo;              
  4.     $bar = "My name is $bar";  
  5.     echo $bar;
  6.     echo $foo;
  7.     ?>

a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob
View Answer

Answer: c
Explanation: Firstly, the line $bar = &$foo; will reference $foo via $bar. So $bar is assigned value Bob. Therefore $bar = “My name is $bar”; will print My name is Bob($bar=Bob as said before).

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)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) i), ii) and iv)
View Answer

Answer: b
Explanation: echo(), print() and printf() all three can be used to output a statement onto the screen. The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser.

4. What will be the output of the following PHP code?

  1.     <?php
  2.     $color = "maroon";
  3.     $var = $color[2];
  4.     echo "$var";
  5.     ?>
    

a) a
b) Error
c) $var
d) r
View Answer

Answer: d
Explanation: PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via array offset notation. In an array, index always starts from 0. So in the line $var = $color[2]; if we count from start ‘r’ comes at index 2. So the output will be r.

5. What will be the output of the following PHP code?

  1.     <?php
  2.     $score = 1234;
  3.     $scoreboard = (array) $score;
  4.     echo $scoreboard[0];
  5.     ?>

a) 1
b) Error
c) 1234
d) 2
View Answer

Answer: c
Explanation: The (array) is a cast operator which is used for converting values from other data types to array.

6. What will be the output of the following PHP code?

  1.     <?php
  2.     $total = "25 students";
  3.     $more = 10;
  4.     $total = $total + $more;
  5.     echo "$total";
  6.     ?>

a) Error
b) 35 students
c) 35
d) 25 students
View Answer

Answer: c
Explanation: The integer value at the beginning of the original $total string is used in the calculation. However if it begins with anything but a numerical value, the value will be 0.

7. Which of the below statements is equivalent to $add += $add?
a) $add = $add
b) $add = $add +$add
c) $add = $add + 1
d) $add = $add + $add + 1
View Answer

Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction, multiplication, division etc.

8. Which statement will output $x on the screen?
a) echo “\$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;
View Answer

Answer: a
Explanation: A backslash is used so that the dollar sign is treated as a normal string character rather than prompt PHP to treat $x as a variable. The backslash used in this manner is known as escape character.

9. What will be the output of the following PHP code?

  1.     <?php
  2.     function track() {
  3.     static $count = 0;
  4.     $count++;
  5.     echo $count;
  6.     }
  7.     track();
  8.     track();
  9.     track();
  10.     ?>

a) 123
b) 111
c) 000
d) 011
View Answer

Answer: a
Explanation: Because $count is static, it retains its previous value each time the function is executed.

10. What will be the output of the following PHP code?

  1.     <?php
  2.     $a = "clue";
  3.     $a .= "get";
  4.     echo "$a";
  5.     ?>

a) get
b) true
c) false
d) clueget
View Answer

Answer: d
Explanation: ‘.’ is a concatenation operator. $a. = “get” is same as $a=$a.”get” where $a is having value of “clue” in the previous statement. So the output will be clueget.

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:

  • Get Free Certificate of Merit in PHP Programming
  • Participate in PHP Programming Certification Contest
  • Become a Top Ranker in PHP Programming
  • Take PHP Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Which is not the right way of declaring a variable in php

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.