Reply
Is this code snippet correct?
Old 08-27-2008, 10:56 PM Is this code snippet correct?
TWD
Ultra Talker

Posts: 471
Trades: 0
<?php if($_SERVER['PHP_SELF']==index.php)

{echo "<p>This is the home page dude.</p>"; }
else
{echo "<p>This is NOT the home page dude.</p>"; }

?>

I have a php include that I only want to activate on certain pages and I though the $_SERVER['PHP_SELF'] might be the mechanism to do that.
Having some troubles though.
TWD is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 08-28-2008, 01:53 AM Re: Is this code snippet correct?
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Easy PHP Search with Opera
Posts: 1,048
Name: Matt
Location: Irvine, CA
Trades: 0
You're close:
PHP Code:
<?php

if($_SERVER['PHP_SELF'] == 'index.php')
     echo 
"Home Page";
else
     echo 
"Not Home Page";

?>
The only problem was that you forgot to surround index.php with quotes. Anything that is a string should be surrounded with quotes (in most cases). Notice that I also cleaned up your code a bit, I think a full script written in that style would be very difficult to read.
__________________
Tinsology | How to Post Code | EverythingDev
NullPointer is offline
Reply With Quote
View Public Profile
 
Old 08-28-2008, 12:16 PM Re: Is this code snippet correct?
rogem002's Avatar
Super Spam Talker

Latest Blog Post:
Making Zebra Stripes with CSS3
Posts: 837
Name: Mike
Location: United Kingdom
Trades: 0
PHP Code:
<?php
if($_SERVER['PHP_SELF'] === 'index.php'){ // Notice the 3 = bits and the {
     
echo 'Home Page'// ' instead of " 
} else { // The brackets make it easier to debug.
     
echo 'Not Home Page';
}
?>
Would be better.

Though, if you looking to confirm the right page has been included use this code (it's more easy to paste):
// Main file - Start
PHP Code:
define(INSCRIPT"yes"); 
Anything being included
PHP Code:
if(INSCRIPT !== "yes"){
die(
'Sorry, you are accessing this page in the wrong way');

__________________
PHP Code:
Add_Talkupation('rogem002'); // Because sharing is awesome! 
Fix 90% of your problems | Full On Design

Last edited by rogem002; 08-28-2008 at 12:19 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 08-28-2008, 03:04 PM Re: Is this code snippet correct?
wayfarer07's Avatar
NYE-KEE

Posts: 3,136
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
IMO, not using {} brackets on if, else statements, etc, is a bad habit to get into, so I am in agreement with Mike here.
__________________
Wayfarer | jQuery Tooltip Plugin
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-28-2008, 10:49 PM Re: Is this code snippet correct?
TWD
Ultra Talker

Posts: 471
Trades: 0
Thanks guys.

To be honest though, I`ve always been a bit confused about the necessity for using === as opposed to just ==.

Is there a strong argument for the former?
TWD is offline
Reply With Quote
View Public Profile
 
Old 08-28-2008, 11:10 PM Re: Is this code snippet correct?
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Easy PHP Search with Opera
Posts: 1,048
Name: Matt
Location: Irvine, CA
Trades: 0
In this case it won't make a difference. === not only compares the values but also the type.

So 5 == '5' will return true but 5 === '5' won't
$_POST['username'] == '' will return true if the user did not enter his name, or if there is no such field as username

$_POST['username'] === '' will return true only if the user did not enter his name meaning the field must exist

In most cases == will work just like === but keep an eye out for the special cases when the type really matters.

In your case using === does not make any difference. I've read that === has better performance but it is negligible
__________________
Tinsology | How to Post Code | EverythingDev

Last edited by NullPointer; 08-28-2008 at 11:16 PM..
NullPointer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Is this code snippet correct?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.12414 seconds with 13 queries