Setting an undefined value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Setting an undefined value

Post by Bennettman »

Basically, I want to have a value $content set to a certain value if it's not already defined. The only real situation I need this for is viewing the homepage when someone goes straight to index.php with no definition for $content.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Use unset($variable) to set variable undefined. -> This is wrong....

Code: Select all

if(!isset($content) || strlen(trim($content)) {
  //Action for undefined $content
} else {
  //Action for defined $content
}
Last edited by Takuma on Mon Aug 26, 2002 2:44 pm, edited 2 times in total.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

uhm... that was not what he wanted to do...

use an if statement, and the function isset

if(!isset($content)){
$content = "index stuff";
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or

Code: Select all

if (!defined('MY_DEFINE'))
  define('MY_DEFINE', 'my_value');
...
print(MY_DEFINE);
this would be a constant
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Mmkay, gotcha. So !isset is basically "is not set", with the ! sign...

Cheerz :) ::drinks Coke::
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

Code: Select all

if(!$content) { code; }
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

It's sloppy to do that..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

1) PHP Notice: Undefined variable: content in xyz.php on line xyz

2)

Code: Select all

<?php
$content = '0';
if(!$content)
    print('content not set');
?>
Post Reply