I am currently writing some functions for a script I am working on. My problem is, that the way the content is generated is by a php variable.
So, to generate page contents, it must be like this:
$contents .="page content inside this";
If I make an echo
like this:
echo "page content inside this";
then the echoed text will appear on top of the page. Therefore, I must use the $contents.="";
in order to generate page content.
I am currently writing a function which can quickly generate checkboxes. My function looks like this:
function checkbox($name, $value, $checked){
if($checked == 1){
$checked = "checked";
}else{
$checked ="";
}
$contents.="<div class='roundedOne'>
<input type='checkbox' value='$value' id='roundedOne' name='$name' $checked />
<label for='roundedOne'></label>
</div>";
}
When I call the function inside a page, nothing appears:
$contents.="
".checkbox("name","value","1")."
";
I can imagine that the reason nothing happens when I call the function, is that I've used $contents
instead of echo
, but unfortunately that is my only option, as the script is encrypted, so I cannot change the way $contents
behave.
My question is, how can I make the function print with $contents?
You need to declare $contents
as global before using it inside the function (not recommended) -
global $contents;
$contents.="<div class='roundedOne'>
<input type='checkbox' value='$value' id='roundedOne'
name='$name' $checked />
<label for='roundedOne'></label>
</div>";
Or, better yet, you can generate the content from your function and then return it -
function checkbox($name, $value, $checked){
if($checked == 1){
$checked = "checked";
}else{
$checked ="";
}
return "<div class='roundedOne'>
<input type='checkbox' value='$value' id='roundedOne' name='$name'
$checked />
<label for='roundedOne'></label>
</div>";
}
$contents.= checkbox("name","value","1");
Also, you are changing the type of your $checked
parameter inside the function. This style of coding is strongly discouraged. Try not to change the type of variable in this manner, it will save you from a lot of trouble.
使用global $contents
或在函数末尾return $contents
What have you returned from the function. Return the string from the function . or use $content as global variable as oliverbj suggested
Not sure if I understand your issue correctly. Have you try using global ?
<?php
$contents = "string string string";
function checkbox($name, $value, $checked){
global $contents;
if($checked == 1){
$checked = "checked";
}else{
$checked ="";
}
$contents.="<div class='roundedOne'>
<input type='checkbox' value='$value' id='roundedOne' name='$name' $checked />
<label for='roundedOne'></label>
</div>";
}
$contents.=" ".checkbox("name","value","1")." ";
echo $contents;
?>
Return value of $contents
from function.
Using that way you can get value of $contents
inside your page.
like,
function checkbox($name, $value, $checked){
if($checked == 1){
$checked = "checked";
}else{
$checked ="";
}
$contents.="<div class='roundedOne'>
<input type='checkbox' value='$value' id='roundedOne' name='$name' $checked />
<label for='roundedOne'></label>
</div>";
return $contents;
}
You are no returning value from your function.
So you didn't get anything as a response from checkbox
function.
Just needs to return $contents
from function and then echo that result will do for you.
function checkbox($name, $value, $checked){
if($checked == 1){
$checked = "checked";
}else{
$checked ="";
}
$contents.="<div class='roundedOne'>
<input type='checkbox' value='$value' id='roundedOne' name='$name' $checked />
<label for='roundedOne'></label>
</div>";
return $contents;
}
Then,
echo $contents;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.