PHP 7.0 - Variables And Comments

Introduction

This is my second article on PHP 7.0. In this article we discuss variables, comments, and single and double quotes of PHP. Here's my previous article link:

  • Placeholder for an unknown or changing value.
  • We use variables in everyday life without thinking.
  • The variable remains same, but the value changes.

Naming Variables

  • In PHP, variables always begin with $.
  • Letters, numbers, and the underscore are permitted.
  • First character after $ can’t be a number.
  • No space or hyphens are used.
  • Names are case sensitive.

Good Naming Variables

  • Choose a meaningful name
  • Don’t use single letters, except for counters
  • Don’t use cryptic abbreviations
  • Use camel case or underscore for multiple words

    $FirstName--> Not Meaningful
    $First_Name--> Right Way

Practice of Variables

In this step you can see that we declare one variable and assign it string. In the same way in C language we use printf for display and use echo for display. Now when you run program you can see the output which you declared in variable.

Practice of Variable

Comments

  • Comments are those which are ignored by PHP.
  • There are three types of comments.
  • For single line comment we use //,# also
  • For Multiple lines /* Comment */

Practice of Comments

In this step you see that we write three comments and after writing comments we run the program and here's the output as well.

Practice of Comments

Difference between single quotes and double quotes

  • There are a lot of differences between single quote and double quote.

  • The ‘\’ tells PHP that character reads as a string.

    see the output

When you give a variable name“ (double quotes), it gives the value of the variable which you assign at top. But in single it gives another type of output.

output

You can see that when I place the variable name in single quotes it is treated like a string.

run

Escape Sequence

  • Only one escape sequence works in single quotes.
  • All other escape sequences must be in double quotes.
  • Otherwise, the backslash is treated as literal text.

Aren’t Double Quotes Better?

  • Not necessarily
  • Most coding standards recommend single quotes.
  • Avoid overuse of backslashes.
Read more articles on PHP:

Up Next
    Ebook Download
    View all
    Learn
    View all