Jump to content

[SOLVED] The Print() function help


slapdashgrim

Recommended Posts

Hello i am writing a code to display a link with php becuase the link has to go to a page that is named with a variable but i cant get the variable to print! i have tried many things.

here it is

<?php
$m = "Grimslapdash";
if(isset($m)){
} else {
print "<A href=\"$m.php\">$m</a>";
}
?>

i want it to print the link only if the variable is set. and the link will be

<a href="$m.php">$m</a> or <a href="Grimslapdash.php">Grimslapdash</a>

 

all it does now is print

<a href=".php"></a>

the variable $m is not printing

Link to comment
https://forums.phpfreaks.com/topic/60288-solved-the-print-function-help/
Share on other sites

<?php
$m = "Grimslapdash";
if(isset($m)){
} else {
print "<A href=\"$m.php\">$m</a>";
}
?>

for a start it should show nothing because your code asks if $m is set then do nothing else create the link

 

try this one

 

<?php
$m = "Grimslapdash";
if(!isset($m)){
} else {
print "<A href=\"$m.php\">$m</a>";
}
?>

 

works on my system

if you look at my offering you will see what happened

 

if you set a variable with a value and then check to see whether it is set using isset() it will either be true or false depending ( in this case TRUE because you set it with slapdashgrim)

 

in your code you checked to see whether it was set

if(isset($m))
{
} 

 

which says if the variable IS SET then do nothing because there is no associated code between the curly braces

 

your code goes on to

else 
{
print "<a href="'.$m.'.php">$m</a>";
}

 

to say if it didnt exist create this link with the variable that does not exist ergo

 

<a href=".php"></a>

 

what you needed was the negater "!" in there to say if it DOES NOT exist do nothing ELSE create the link

so the final code should be

<?php
$m = "Grimslapdash";
if(!isset($m)){
} else {
print "<A href=\"$m.php\">$m</a>";
}
?>

 

hope that helps

 

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.