The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > PHP


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 08-08-2007, 02:07 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
[SOLVED]=== what does it do?

As the title says what does === mean when you use it in php?

Theres = for assigning things and theres == for equal to, the best description i have so far is...

PHP Code:
if(somebooleanfunction($var) === TRUE){
//Statements

That would work where as:
PHP Code:
if(somebooleanfunction($var) == TRUE){
//Statements

Would not work, is that right?

Can anyone say where else === can be used and why it should be?

Thanks for any help.
Reply With Quote
  #2 (permalink)  
Old 08-08-2007, 03:07 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 417
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
While not easy to find, this will be helpful: PHP: PHP type comparison tables - Manual

Let me know if you have any other questions.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to TeraTask For This Useful Post:
Lee (08-08-2007)
  #3 (permalink)  
Old 08-08-2007, 03:15 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Quote:
Originally Posted by TeraTask View Post
While not easy to find, this will be helpful: PHP: PHP type comparison tables - Manual

Let me know if you have any other questions.
That does help quite a bit, i am just trying to get my head round it all, when i come to use it soon i am now confident i will be able to use it.

Thanks.
Reply With Quote
  #4 (permalink)  
Old 08-08-2007, 03:26 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
I recently used === in a PHP script of my own. One area where === is useful is true/false v.s. non-zero and non-false values. Any non-false and non-zero value is considered true. In other words, if you are testing for a boolean value to be returned from a function (i.e. a PHP function returns FALSE on error, but its return value is defined as int), you should use === instead of == .

In fact, there is a noticeable warning present in the documentation for the strpos function. For those that don't feel like loading a page (especially those who still use dial-up or have a similar slow connection), here is the warning:
Quote:
Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
PHP Code:
$needle 'hELLO';
$haystack 'Hello World!';
$pos strpos($haystack$needle);
if (
$pos === false)
    echo 
"$needle could not be found in $haystack.";
else
    echo 
"$needle was found at position $pos in $haystack.";

$needle 'Hello';
$pos strpos($haystack$needle);
if (
$pos === false)
    echo 
"$needle could not be found in $haystack.";
else
    echo 
"$needle was found at position $pos in $haystack."

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
Lee (08-08-2007)
  #5 (permalink)  
Old 08-08-2007, 03:54 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
So are you saying that instead of:
PHP Code:
if ($var == $var2){
//do things
}

//i should use...?

if ($var === $var2){
//do things

Reply With Quote
  #6 (permalink)  
Old 08-08-2007, 04:06 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 417
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
It just depends on the type of information that you're trying to compare and how you want to compare it.

PHP Code:
//Let's say we have this function
//NOTE: All functions do what they say they'll do, even if not defined.
function number_of_members_attending($meeting_id) {
  if (
meeting_exists($meeting_id)) {
    
//Now, calculate the number of members and store in $number_of_members

    
return $number_of_members;
  } else {
    
//Meeting doesn't exist, so this question is inappropriate
    
return false;
  }

Now, this function will return 3 possible values of interest:

1) false if the meeting_id is invalid or no meeting exists;
2) 0 if the meeting has no registered users;
3) > 0 if the meeting has some number of members.

Now, let's say that you were writing code which uses the function above:

PHP Code:
$members_attending number_of_members_attending(20070315);
if (
$members_attending) {
  echo 
"There are ".$members_attending." members attending your meeting.";
} else {
  echo 
"No meeting is scheduled.";

The code above would be inaccurate as a meeting may be scheduled and no members set to attend yet, so you could be more explicit by using:

PHP Code:
$members_attending number_of_members_attending(20070315);
if (
$members_attending === false) {
  echo 
"No meeting is scheduled.";
} elseif (
$members_attending == false) {  //I'm just doing that as an example, normally I'd use $members_attending == 0
  
echo "No one is scheduled to attend this meeting yet.";
} else {
  echo 
"There are ".$members_attending." members attending your meeting.";  

By using identically equal ( === ) I am able to specifically distinguish between false and 0 which matters in this case (and, also, in the strpos case).

I hope that helps!

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to TeraTask For This Useful Post:
Lee (08-08-2007)
  #7 (permalink)  
Old 08-08-2007, 04:21 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Quote:
Originally Posted by TeraTask View Post
I hope that helps!
That helped out great, i think i can now say i understand it fully as i ever will do, if i have any problems i will get back to you all.

Thanks
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 10:56 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


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