Webmaster Forum
Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs

UK Web Hosting
UK Web Hosting
Website Hosting
Website Hosting
UK One Way
UK One Way
Free Website Thumbnail Creator
 
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 11:47 AM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default Tip : difference between =, == and === in php

Confusion between = and == is usually a frequent source of error in scripts written by newbies. This often hard to detect (for newbies) problem arises when they use = assignment operator thinking of it as a comparsion operator in the if control stucture. For example, take a look at the following example :

Code:
<?php
  $user_input = $_GET['user_input'];
  
  if($user_input = 5)
  {
    echo 'You entered 5';     
  }
?>
When executed, the above code will always print "You entered 5" regardless of the input given by the user. The reason, of course, is the incorrect use of assignment operator instead of comparison operator in the if conditional expression. The assignment operator replaces the previous value stored in the $user_input variable with 5 and then it is converted to a boolean value which comes out to be true and hence the statement in braces is always executed. The correct code is :

Code:
<?php
 $user_input = $_GET['user_input'];
  
  if($user_input == 5)
  {
    echo 'You entered 5';     
  }
?>
running above code, it'll print "You entered 5" only when the user actually gives 5 as input.

=== is also a comparison operator similar to == as both check for operand equality, but it is a bit more stricter. == operator considers its operand as equal when both of them have same value whereas === operator will consider them equal only when they have same type as well. For example,

Code:
<?php
   $value1 = 7;
   $value2 = "7";
   
   if($value1 == $value2)
   {
     echo 'According to == operator, $value1 and $value2 are equal.';
   }
   
  if($value1 === $value2)
  {
    echo 'According to === operator, $value1 and $value 2 are equal.';
  }
?>
When the above code is executed, only first echo statement will be executed.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 05-29-2008, 08:11 AM
garima's Avatar
Junior Member
 
Join Date: May 2008
Posts: 17
iTrader: 0 / 0%
garima is on a distinguished road
Default

Two of the many comparison operators used by PHP are '==' (i.e. equal) and '===' (i.e. identical). The difference between the two is that '==' should be used to check if the values of the two operands are equal or not. On the other hand, '===' checks the values as well as the type of operands.
__________________________________________________ _____________
Web Site Design
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks

Webmaster Resources
UK Web Hosting
UK WW SEO Tools
Free site submission
Web Directory
 
Advertisement
Get top 10 exposure
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
2 column 2 columns 301 addtype advice bbpress bid bidding directory blog post british telecom broadband cheap chip maker community coupon code designs directory domain for sale domain name ecommerce ecommerce information ecommerce poll edegra fibre optic forum fraud free free portal script free script generate revenue generic viagra google graphics heaven intel internet spending james in london kamagra keywords layout link exchange links wanted linux and windows server local search modeling monopoly online retail online spending optimising owg in london parking photography php picture of the day purple robots.txt scour scripts search engine search quality site promotion special discount speed cameras submission theme usability web hosting website win a network wordpress

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 10:51 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150