Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Is this code snippet correct?
Old 08-27-2008, 10:56 PM Is this code snippet correct?
TWD
TWD's Avatar
King Spam Talker

Posts: 1,111
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 online now
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-28-2008, 01:53 AM Re: Is this code snippet correct?
NullPointer's Avatar
Will Code for Food

Posts: 2,781
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-28-2008, 12:16 PM Re: Is this code snippet correct?
rogem002's Avatar
PHP Chap

Posts: 843
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');

__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

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
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
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.
__________________
Join me on
Please login or register to view this content. Registration is FREE
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
TWD's Avatar
King Spam Talker

Posts: 1,111
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 online now
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

Posts: 2,781
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
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 08-28-2008 at 11:16 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
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 1.09749 seconds with 12 queries