Dailytip.net

Articles on pretty much anything.

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008


Beginners PHP: Comparing strings

Starting out in PHP can a daunting task. One of the most important things to do when starting a new language is to get the basics right. One of the most basic things you can do in PHP is compare a string; which is exactly what I aim to show you in this short post. Click more to find out just that.

During this example i'm going to assume you know, or are at least familiar with, the PHP syntax. 

   1:  <?php
   2:      $stringA = "DailyTip is cool";
   3:      $stringB = "DailyTip sucks";
   4:      $stringC = "DailyTip is cool";   
   5:  
   6:      if($stringA === $stringB)   
   7:      {
   8:         echo "That can't be right!!";
   9:      } 
  10:   
  11:      if($stringA === $stringC)
  12:      {
  13:         echo "These strings are the same.";
  14:      }  
  15:  ?> 

Let's disect that code - line by line - and look at what it does.

Line 1: Start PHP.

Line 2: Here we declare our first string.

Line 3: Time to declare the second string.

Line 4: And the 3rd string gets declared

Line 6 to 9: This is an if statement.  It's a little more tricky than the code we've handled so far. Nothing too hard though.. What we're saying here is "If string A is IDENTICLE TO string B, tell us that can't be right". The way we say "identicle to" in PHP is by using the identicle operator which is 3 equals signs in a line ("==="). 

Line 11 to 14: Another if statement. Here we are comparing string A and string C using the identicle operator that we used on line 5. If the two strings are the same, then we echo out "These strings are the same." - which proved true by our if statement.

Line 15: End PHP.

I hope this little tutorial helped you to start grasping the basics of PHP. Take a look around Daily Tip and you'll find numerous other bits of code and interesting things about programming and scripting lanugages.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Andy on Wednesday, March 05, 2008 10:39 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Wednesday, November 19, 2008 10:56 AM