Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Introduction to the PHP-syntax for starting web-developers

Monday, August 18th, 2014 by Servage

Variable in PHPPHP is a web-programming language that is easy to learn and implement. It is also a zero investment language due to its open-source license with a vast community to help if you are a beginner, learning a server-side script for your first time. Web programmers don’t need any special code editor as PHP code can integrate with any language and markup, including HTML. If you look at a PHP file, you will see simple text, HTML, CSS, JavaScript, and other scripting with the PHP code.

Being a server-side language, PHP code gets executed on the server and delivers HTML to the client. Therefore, it adds no load to clients and has proven itself as a highly optimized script for the biggest websites (WordPress like blogs), largest social network (Facebook), as well as enterprise-level web applications.

Because it is responsible for dynamic and interactive web page creation, nearly half of all websites online are comprised of it. If you are a newbie to the software programming community, here are some simple instructions to get into PHP coding.

Simplicity in PHP Syntax

As installation of PHP is fairly simply and only needs a supporting server, a MySQL-like database, and few configurations, so getting started with PHP is easy. Similarly, coding with PHP is smooth sailing because you need not make a great departure from simple HTML and other C class languages in terms syntax rules and formation. For instance, PHP is not case-sensitive for any of its user-defined functions, classes, keywords, etc., though all variables in PHP are case-sensitive. A PHP script along with any comments you write, begins in three different ways: // or # are for single line comments, and /*…*/ for multiple line comments.

Variables in PHP

As mentioned above, variables in PHP are case-sensitive containers for storing data that perform math-like algebraic functions. See example below:

<!--DOCTYPE html>

In the above code, the PHP variable begins with $ (pound) sign, followed by the name of variable (x-here). It holds values (i.e. x=5) or shows an expression (i.e. z=x+y). One thing to remember in coding of a variable is that you don’t begin with numbers. Variables only include alpha-numeric characters or an underscore sign as the name of the variables.

Unlike other languages, you don’t need any special command to declare the variable, as it was created as soon as you assigned the value to it. Of course, you have to use quotes around the value of the variable, if that value is in text form. Moreover, you don’t need to define the data type for a variable because PHP automatically select this based on a defined value.

Generally in PHP, you find three different scopes for the variable: local, global, and static. However, the scope of variable depends on the position of the function in PHP. For instance, if a variable is declared outside the function, it will become a global scope. Whereas a declaration inside the function, becomes a local scope. See the example below:

<!--DOCTYPE html>
<!--?php
$x=5; // global scope

function myTest() {
  $y=10; // local scope
  echo "Test variables inside the function:";
  echo "Variable x is: $x";
  echo "";
 echo "Variable y is: $y";
}

myTest();

echo "Test variables outside the function:";
echo "Variable x is: $x";
echo "";
echo "Variable y is: $y";
?>

You may have noticed that local variables only print the value when they are in the function. This means you can choose the same name for local variable, if they are placed in different functions.

References & More Reading
PHP 5 Introduction
Variables

Introduction to the PHP-syntax for starting web-developers, 4.7 out of 5 based on 3 ratings
Categories: Guides & Tutorials, Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.