PHP T utorial for Beginners – Part 2 by hsits


									                                                                                                             GRAB THE RSS            POSTS BY EMAIL

  Home     Archives     About          Contact   WP Themes                                                                                  Search

Categories          PHP T utorial for Beginners – Part 2                                                Sponsors
                    by Daniel Scocco
Blog Design
                                                                    If you missed it, here’s the link
Blog Projects
                                                                    to Part 1. So far we covered the
Bloggers Face-Off                                                   basics of PHP, how to run your
Blogging Basics                                                     programs, strings and variables.
Blogosphere                                                         Today I’ll talk about operators.
Domain Names
                                                                    An operator, as you certainly
General                                                             know from when you studied
Link Tips                                                           maths, is a symbol representing
Monetize                                                            a certain operation. Programming
Promotion                                                           languages, however, how many
                                                                    different types of operators, not
                                                                    only the arithmetic ones, and
SEO                 below I’ll talk about the basic ones.
Social Media
Software            1. Arithmetic Operators
Strategy            PHP (and most other programming languages) has all the basic arithmetic
                    operators you are familiar with. Addition is represented by +, subtraction by -,
Video Posts
                    division by / and multiplication by * . So the following piece of code:
Web Tools
WordPress             <?php
Writing Content
                      $x = 1 + 1;
                      $y = $x * 4;                                                                      Popular Articles
                      $y = $y / $x;                                                                     28 Ways to Make Money with Your
                      $y = $y - 10;                                                                     Website
                      echo "$x and $y\n";
                      ?>                                                                                43 Web Design Mistakes You Should Avoid
                                                                                                        4 Steps to Increase Your Blog Traffic
                    would print “2 and -6″. If that is not 100% clear to you make sure to go over       How to Find Advertisers for Your Website
                    each stop slowly until you get what’s going on. Notice that when the same
                                                                                                        Top 25 SEO Blogs
                    variable appears both on the left and on the right side of an attribution, first
                    the right side is computed (using the value currently stored in the variable),      101 Blog Tips I learned in 2006
                    and only after that the attribution is performed, changing the value of the         30 Traffic Generation Tips
                    variable.                                                                           6 Ways to Speed Up Your Site
                    As you probably also noticed the = sign is for attribution. If you want to          9 Hacks to Show Readers Your Best Stuff
                    evaluate the equality of two variables you need to use == (more on that under       50 Simple Ways to Gain RSS Subscribers
                    the “Relational Operators” section).                                                The Best Website Taglines
                    One very useful operator you might not be familiar with is the modulus one,         What Is Success?
                    represented by the % symbol. Basically x % y will return the remainder when x       How to setup a 301 Redirect
                    is divided by y. For instance:                                                      The 7 Characteristics of Good Domains

                      <?php                                                                             7 Ways to Promote your Site
                      echo 14%10;                                                                       Top 25 Celebrity Blogs
                                                                                                        What Is A Blog?
                    Prints 4, because 14 divided by 10 is 1 and has a remainder of 4.                   Blog Setup: 40 Practical Tips
                                                                                                        10 Tips To Write Your Most Popular Post
                    Why is this operator useful? Because it allows you to understand the properties
                                                                                                        The Blog Post Checklist
                    of some numbers and to adapt them to your requirements. For instance,
                    suppose you want to know if any given number is even or odd. You simply need        Interview: 12 Top Online Entrepreneurs
                    to make that number modulus 2. If the result is 0 the number is even, if the        What Is Bounce Rate?
                    result is 1 it’s odd.                                                               20 SEO Terms You Should Know
                    Second, suppose you are working with hours. You have a variable that you            How To Choose A Blog Name
                    keep adding to, but you don’t want it to go past 24. You just need to keep          10 Tips to Sell Your Website on Flippa
                    adding and every time you do so you also take the modules 24 of the result, so      Top 25 Web Design Blogs
                    once it reaches 24 it will go back to 0.

                    2. Relational Operators
                    Sometimes these are also called comparison operators, as they compare two
                    different entities (i.e., numbers, variables, strings, etc).

                    They are:

                       $a > $b (true if $a is greater than $b)
                       $a >= $b (true if $a is greater than or equal to $b)
                       $a < $b (true if $a is less than $b)

                                                                                                                                 converted by
    $a   < $b (true if $a is less than $b)
    $a   <= $b (true if $a is less than or equal to $b)
    $a   == $b (true if $a is equal to $b)
    $a   != $b (true if $a is not equal to $b)
    $a   === $b (true if $a is equal to $b and has the same type)
    $a   !== $b (true is $a is not equal to $b, or if they are not of the same type)

So the following piece of code:

 echo 5>4;
 echo "\n";
 echo 5==4;
 echo "\n";
 echo 5==="5";
 echo "\n";
 echo 4>=4;
 echo "\n";

Outputs this:



That’s a true, false, false, true. The boolean true is converted to the string
“1″, while the boolean false is converted to an empty string “”, that’s why only
1s were printed. False is equivalent to 0 as well, though.

3. Logical Operators
The three basic logical operators are:

    $a && $b (called the AND operator, it returns true if both $a and $b are true
    (i.e., true here means not zero)
    $a || $b (called the OR operator, it returns true if either $a or $b are true
    !$a (called the NOT or negation operator, it returns true if $a is false (i.e.,

So the following piece of code:

 $a = 12323;
 $b = 0;
 echo $a&&$b;
 echo "\n";
 echo $a||$b;
 echo "\n";
 echo !$a;
 echo "\n";



That is, false, true and false. Again, if you don’t see why go over each step
until you understand it.

There are the three basic types of operators, and with them you can already
create most programs. If you want to read about the other types of operators
visit this section of the PHP manual.

Related Posts                                                    Share
    PHP Tutorial For Beginners – Part 1
    Useful Google Operators
    Using the “linkfromdomain” Operator on Bing To Find Spammy      Tweet     Like
    Links on Your Site

             Sign-up To The Newsletter And Get A Free eBook
     Sign-up to the Daily Blog Tips newsletter and
     you will be able to download the "Make Money
     Blogging" eBook for free (worth $47).
     You will also receive tips to improve your blog,
     strategies to make money and useful resources
     from around the web.

                Nam e
                Em ail

                                                                                       converted by
6 Responses to “PHP Tutorial for Beginners – Part 2”
raj on February 29, 2012 3:02 pm

great…now i am confident that i can learn php.

I tried one year ago but found it was too difficult for me.

Trevor on February 29, 2012 5:17 pm

Its awesome that you’re sharing this information about PHP, and getting it in
small chunks like this is the best way for people to learn.

It’s pretty difficult to do any kind of major customization of your WordPress site
or blog without a solid understanding of PHP. I plan to follow along as a

Rahul kuntala on March 1, 2012 4:22 am

I am enjoying your posts, especially the PHP tutorials. Great way to learn.

Thanks for sharing

Leif G.S. Notae on March 1, 2012 9:19 am

Ugh, I keep forgetting to tackle this. Thanks for the reminder, looks as though
I’ll have more work in my work heh

Thanks for sharing, great article.

Sohel on March 1, 2012 9:39 am

awesome tips. thanks for shearing.

Ivan on March 5, 2012 2:21 pm

Nice tutorial !

                  Home - Advertise - Archives - Contact - Resources - Privacy Policy - RSS

                                                                                             converted by

To top