Sunday, April 14, 2013

Variables


All PHP variables begin with a dollar sign '$' followed by the variable name. Variable names must always begin with either a letter or an underscore '_'.
Example:
$variable_name
$variableName
$VAR
$Var
$var1
$_var
All the examples above are valid variable names. PHP variables are case sensitive so $var and $Var are two different variables. 
Now, let us look at some invalid variable names.
$_1var
$1var
$var!^
Variable names cannot begin with a number or an underscore followed by a number. Variable names cannot also contain any character other than numbers (0-9), letters (a-z, A-Z) and underscores(_).


Naming Conventions


There are two main naming conventions used in naming PHP variables. 
First, the Camel case where the second and the succeeding words are started with a capital letter.
$variableName
$thisIsVariable
$awesomeVariableName1

Second, all letters are in small caps and the words are separated by underscores.
$variable_name
$this_is_a_variable
$awesome_variable_name1

It does not really matter what naming convention you use unless you are required to use a particular one. The most important thing to remember is that you must always be consistent in naming your variables. If you use Camel case in a particular project, then use it all through out the project.

Another important thing to remember when naming variables is that you must give meaningful variable names. Variable names are usually made out of nouns. Do not just name your variables $x, $y, $var1, $var2. Make an effort to give them meaningful names because it makes the code easier for you and most importantly, for others to read. Name your variable according to its purpose. If a variable will hold the sum of two numbers, then you can name it $sum;  if a variable will hold a name of a person, then you can name it $personName. I know it is hard at first but as time goes by, it will become natural and you will definitely appreciate it. Therefore, you must practice those things as you start learning PHP.


Variable Declaration and Initialization


When assigning a value to a variable, the assignment operator is used '='. The syntax for assigning a value to a variable is 
variable name = value;
Make sure you put a semi-colon ';' at the end of the line. Every PHP statement must always end in a semi-colon or else, you will get an error.
Here are some examples:
<?php
$var = 1;
$var2 = "hello world";
$price = 3.1;
?>
There are three main data types that you can use in your variables, these are: integer, double and string. If you are confuse on what data types are, do not worry because we will discuss it later in another tutorial.

PHP is a weakly-typed language.This means variables' data type are not predefined so it can contain any type of data. You can change the data type of a variable any time by assigning a value with a different data type. PHP will automatically determine the type of the variable depending on the value assigned to it.
Example:
<?php
$var = 1;
$var = "hello world";
$var = 3.1;
?>
In the first line of code, $var is an integer. In the second line, "hello world" which is a string is assigned to $var. Therefore, its type is now a string. In the last line of code, 3.1, a double, is assigned to $var so its type now is change into a double. 


Printing the Value of a Variable


The syntax for printing the value of a variable is this:
<?php
echo $var;
?>
In the above code, $var was not given a value so the code will not print anything. Let us look at another example.
<?php
$var = "hello!";
echo $var;
?>
In this case, $var was given a value. The output will be:
hello!
Let us now look at the output that our previous example about type juggling will give.
<?php
$var = 1;
$var = "hello world";
$var = 3.1;
echo $var
?>
Output:
3.1
As you can see, the value of a variable is the last one that is assigned to it.


Wednesday, March 20, 2013

Tree traversal is used to print data in a binary tree. There are two main types of tree traversal. Depth-first and Breadth-first. Depth-first traversal can be further divided into three types: pre-order traversal, in-order traversal and post-order traversal. Lets get started.



Depth-first

Pre-order Traversal

  • Visit the root.
  • Traverse the left subtree in pre-order
  • Traverse the right subtree in pre-order

In-order Traversal

  • Traverse the left subtree in in-order
  • Visit the root.
  • Traverse the right subtree in in-order

Post-order Traversal

  • Visit the root.
  • Traverse the left subtree in post-order
  • Traverse the right subtree in post-order

Those the main ideas behind the three depth-first traversals. To give you a clearer understanding, here are the basic algorithms for each traversal.

Pre-order traversal algorithm



In-order traversal algorithm




Post-order traversal algorithm



Breadth-first

Level-order Traversal

  • Visit the nodes starting from the top until the bottom. Print them from left to right.



Now, let us look at some examples. 

1.


Pre-order sequence: A B D G E C F
In-order sequence: G D B E A C F
Post-order sequence: G D E B F C A
Level-order sequence: A B C D E F G



2.


Pre-order sequence: A B D E G C F H
In-order sequence: D B G E A C F H
Post-order sequence: D G E B H F C A
Level-order sequence: A B C D E F G H


3.

Pre-order sequence: A B D F H I E G C
In-order sequence: F H I D B G E A C
Post-order sequence: I H F D G E B C A
Level-order sequence: A B C D E F G H I




If you followed the algorithms correctly, you would get the same answers. You could practice using those algorithms until you get a hang of it. I will add more examples in the future so that you can practice more cases. I will also post another tutorial about traversals that will teach you how to output the different sequences in a fast and simple way. But for now, that's all.



    Hello! Welcome to Code Realm.

    This is a site where you can learn different programming languages such as HTML, CSS, PHP, Java, C and C++. I will also teach some algorithms and post random tips.

    I know there are already tons of websites that teaches those things but I will teach it in a way beginners will easily understand. Most of the programming tutorial sites I visit teaches the concepts but they do not usually give adequate examples for the people to understand the concepts. It is true that you can learn by reading the concepts but examples give people a better understanding on how it works. Therefore, I will give lots of examples that is practical and will make you easily comprehend the things I will teach.

    Be sure to check this site for the tutorials that I will post.

    Tuesday, March 19, 2013


    This Privacy Policy governs the manner in which coderealm.blogspot collects, uses, maintains and discloses information collected from users (each, a "User") of the http://coderealm.blogspot.com/ website ("Site"). This privacy policy applies to the Site and all products and services offered by coderealm.blogspot.

    Personal identification information

    We may collect personal identification information from Users in a variety of ways in connection with activities, services, features or resources we make available on our Site.. Users may visit our Site anonymously. We will collect personal identification information from Users only if they voluntarily submit such information to us. Users can always refuse to supply personally identification information, except that it may prevent them from engaging in certain Site related activities.

    Non-personal identification information

    We may collect non-personal identification information about Users whenever they interact with our Site. Non-personal identification information may include the browser name, the type of computer and technical information about Users means of connection to our Site, such as the operating system and the Internet service providers utilized and other similar information.

    Web browser cookies

    Our Site may use "cookies" to enhance User experience. User's web browser places cookies on their hard drive for record-keeping purposes and sometimes to track information about them. User may choose to set their web browser to refuse cookies, or to alert you when cookies are being sent. If they do so, note that some parts of the Site may not function properly.

    How we use collected information

    coderealm.blogspot may collect and use Users personal information for the following purposes:
    • - To personalize user experience
      We may use information in the aggregate to understand how our Users as a group use the services and resources provided on our Site.
    • - To send periodic emails
      We may use the email address to respond to their inquiries, questions, and/or other requests.
    How we protect your information

    We adopt appropriate data collection, storage and processing practices and security measures to protect against unauthorized access, alteration, disclosure or destruction of your personal information, username, password, transaction information and data stored on our Site.

    Sharing your personal information

    We do not sell, trade, or rent Users personal identification information to others. We may share generic aggregated demographic information not linked to any personal identification information regarding visitors and users with our business partners, trusted affiliates and advertisers for the purposes outlined above.We may use third party service providers to help us operate our business and the Site or administer activities on our behalf, such as sending out newsletters or surveys. We may share your information with these third parties for those limited purposes provided that you have given us your permission.

    Third party websites

    Users may find advertising or other content on our Site that link to the sites and services of our partners, suppliers, advertisers, sponsors, licensors and other third parties. We do not control the content or links that appear on these sites and are not responsible for the practices employed by websites linked to or from our Site. In addition, these sites or services, including their content and links, may be constantly changing. These sites and services may have their own privacy policies and customer service policies. Browsing and interaction on any other website, including websites which have a link to our Site, is subject to that website's own terms and policies.


    Changes to this privacy policy

    coderealm.blogspot has the discretion to update this privacy policy at any time. When we do, we will revise the updated date at the bottom of this page. We encourage Users to frequently check this page for any changes to stay informed about how we are helping to protect the personal information we collect. You acknowledge and agree that it is your responsibility to review this privacy policy periodically and become aware of modifications.

    Your acceptance of these terms

    By using this Site, you signify your acceptance of this policy. If you do not agree to this policy, please do not use our Site. Your continued use of the Site following the posting of changes to this policy will be deemed your acceptance of those changes.

    Contacting us

    If you have any questions about this Privacy Policy, the practices of this site, or your dealings with this site, please contact us at:
    coderealm.blogspot
    http://coderealm.blogspot.com/
    coderealm.blogspot@gmail.com

    This document was last updated on March 22, 2013

    Privacy policy created by Generate Privacy Policy