Comparing the Basic Syntax in three Languages: Javascript, PHP, and Python (Part 1)

One of my new year resolutions is to review Python and PHP languages. Could you believe January is almost gone, and I just started on the basic syntax of Python yesterday? Not mentioned I have not lost any weights yet? I have a pretty worry feeling about my new year’s resolution.

Anyway, I started the most popular language – Python first, cause I like to follow the trend. I was reviewing the rules of variables names, then I asked myself, Python can’t use $ sign? Which language has to use $ for variables? Could Javascript use $ sign or it is illegal?

Hmm…

If you don’t use it you lose it.

Maybe it is time to review all three languages together and find these little differences among and maybe a place that I could refer to in the future. That was how I started the idea to create this series of blog posts. Let’s begin!

How to comments on codes?

  • // and # is used for one-line comment
  • /* Words */ is used for multi line comment
  • # is used for one-line comment
  • “””   Words “”” is used for multi-line comment
  • // is used for one line comment
  • /*  Words */ is used for multi-line comment

How to output text, numbers or other printable information to the console?

Print():

  • print(“Hello World”);
  • print “foo is $foo”;

Print():

  • print(“Hello World”)
  • print(100)
  • print(pi)
  • print(“She lives with ” + name)
  • alert() — Output data in an alert box in the browser window
  • confirm() — Opens up a yes/no dialog and returns true/false depending on user click
  • console.log() — Writes information to the browser console, good for debugging purposes
  • document.write() — Write directly to the HTML document
  • prompt() — Creates a dialogue for user input

What are the rules to create variable name?

  • 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)
  • 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, Age and AGE are three different variables)
  • Variable names must begin with a letter, an underscore (_) or a dollar sign ($).
  • Variable names can only contain letters, numbers, underscores, or dollar signs.
  • Variable names are case-sensitive.

How many data types in total?

    According to PHP Manual, it supports ten primitive types.

    • Four scalar types
      • boolean
      • integer
      • float (floating-point number, aka double)
      • string
    • Four compound types
      • array
      • object
      • callable
      • iterable
    • Two secial types
      • resource
      • NULL

    gettype function can be used to check a variable’s type.

    Example:

    $number = 5/0;

    echo $number.”&ltbr /&gt”;

    Python has six standard data types:

        • Numbers
          • Integer
          • Float
          • Complex
        • String
        • List
          • A list can contain a series of values
          • List variables are declared by using brackets []. A list is mutable, which means we can modify the list.
        • Tuple
          • A tuple is a sequence of Python objects separated by commas.
          • Tuples are immutable, which means tuples once created cannot be modified. Tuples are defined using parentheses ().
        • Set
          • A set is an unordered collection of items. Set is defined by values separated by a comma inside braces { }.
        • Dictionary
          • Dictionaries items are stored and fetched by using the key. Dictionaries are used to store a huge amount of data. To retrieve the value we must know the key.
          • In Python, dictionaries are defined within braces {}.

    type function can be used to check a variable’s type.

    Example:

    a = 5

    print(a, “is of type”, type(a))

    There are a total of seven types. Six types of them are primitive and immutable.

    • Boolean – true or false
    • Null — no value
    • Undefined — a declared variable but hasn’t been given a value
    • Number — integers, floats, etc
    • String — an array of characters i.e words
    • Symbol — a unique value that’s not equal to any other value – Symbols are values that programs can create and use as property keys without risking name collisions.
    • Everything else is an Object type

    How to concatenate strings to create sentences?

    The first is the concatenation operator (‘.‘), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘.=‘), which appends the argument on the right side to the argument on the left side.

    Examples:

      $string1 = $name.” is having fun!”;

      $string1.=” That is great!”;

      echo $string1;

     

    Python uses “+” operators to concatenate string.

    One thing to note is that Python cannot concatenate a string and integer. These are considered two separate types of objects. So, if you want to merge the two, you will need to convert the integer to a string.

     

    Examples:
    print ‘red’ + ‘yellow’
    print ‘red’ + str(3)

    • The + operator does string concatenation, and you also can use += as well.
      • Example

    “Say hello ” + 7 + ” times fast!”

     

    • Or collect the strings to be concatenated in an array and join it aferwards.
      • Example

                 var arr = [];
                 arr.push(“Say hello “);
                 arr.push(7);
                 arr.join(“”)

    Share This