Jump to content

[SOLVED] isset problem or missunderstanding


Siggles

Recommended Posts

Further up my code a variable is filled with a string.

 

Eg, $text = 'thanks';

 

Later on i the code I write..

 

if (isset($text)) {
echo $text;
}

 

Have I got the wrong understanding of how isset works? If I just put echo $text it works but I wanted to use isset to broaden what I am doing.

yeah, thats fine how you're using it. But a more common way to use isset() is for variables passed from 1 page to another. by either POST or GET method.

 

for example:

<?php

if(isset($_GET['text'])) {
echo $_GET['text'];
} else {
echo "text is not set!";
}

?>

yeah, thats fine how you're using it. But a more common way to use isset() is for variables passed from 1 page to another. by either POST or GET method.

 

for example:

<?php

if(isset($_GET['text'])) {
echo $_GET['text'];
} else {
echo "text is not set!";
}

?>

 

Basically, someone submits a form to the same page, then a switch statement at the top of the page proccesses the POST variables and the result of that populates the text varaible with a message. I wanted to use something like isset so that I can add html in if it is set and not, if it isn't.

isset checks for a true or false variable,

the way u fill in u check if there is a variable $text, wich is in this case ALWAYS true so ur code works

 

i also agree on master that his way is more common

 

then i still dont understand why it doesn't echo to the screen. Here is snippets of the full code.

 

if (eregi('^[0-9]$', $presultmfc) && eregi('^[0-9]$', $presultother)) {
mysql_query("INSERT INTO predictions (datecreated, username, id, resultmfc, resultother, score, lasteditedby) VALUES ('$datecreated', '$pusername', '$pid', '$presultmfc', '$presultother', '$pscore', '$adminlgd')");
$formsubtext='Enter a number between 0 and 9';
} else {
$formsubtext='Prediction Submitted';
}

 

then later in the code...

 

<tr>
		<td colspan="4" align="left" class="style6" style="padding: 2px">Enter the User's Prediction below...</td>
	</tr>
	<?php 
	if (isset($formsubtext)) {
	echo "<tr><td>";
	echo $formsubtext;
	echo "</td></tr>";
	}
	?>
	<tr>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.