This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I'm new to PHP, please be gentle.
What do I need to change to make this work in PHP?
<div>some HTML here</div>
<?php
function typ__1() {
if ($temperature >= 29) {
$hot = true;
} else {
$hot = false;
}
}
?>
<?php foreach (array_slice($data->something->something, 0, 5) as $day):
$temperature = $day->temperature;
typ__1();
if ($hot == true) {
$bottom = "Shorts";
} else if ($hot == false) {
$bottom = "Pants";
}
<div><?php echo $bottom ?></div>
<?php endforeach ?>
So the main issue/question is if I'm using the function correctly. Can I write if-statements in an outside function and then use them inside a
foreach-loop? The reason/goal is to shorten the foreach-loop.
(This is a reduced example, so there could be a typo somewhere in there.)
Thanks for your help!
Everything is about scope of PHP variables. You should "inject" variable into function like this:
<div>some HTML here</div>
<?php
function typ__1($temperature) {
if ($temperature >= 29) {
return true;
}
return false;
}
?>
<?php foreach (array_slice($data->something->something, 0, 5) as $day):
if (typ__1($day->temperature)) {
$bottom = "Shorts";
} else if (typ__1($day->temperature)) {
$bottom = "Pants";
}
<div><?php echo $bottom ?></div>
<?php endforeach ?>
http://php.net/manual/en/language.variables.scope.php
Add parameter in your function and return a value.
<?php
function typ__1($temperature) {
if ($temperature >= 29) {
$hot = true;
} else {
$hot = false;
}
return $hot;
}
?>
<?php foreach (array_slice($data->something->something, 0, 5) as $day):
$temperature = $day->temperature;
$hot=typ__1($temperature);
if ($hot == true) {
$bottom = "Shorts";
} else if ($hot == false) {
$bottom = "Pants";
}
<div><?php echo $bottom ?></div>
<?php endforeach ?>
Related
How to edit already setted function inside () if I want inside that function if statement
<?php
$a = 1;
function writeMsg($x) {
echo $x;
}
writeMsg(
Hello,
if ($a == 1) {
echo "men";
} else {
echo "women";
}
);
?>
I think you want to prepare the message first then write message.
<?php
$a = 1;
function writeMsg($x) {
echo $x;
}
$message = 'Hello, '. ( $a == 1 ? 'men' : 'women');
writeMsg($message);
SO here is my code:
<?php
$image = '';
if (getUserChar($_SESSION['user_login']) == 1) {
$image = 'warrior';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 2) {
$image = 'mage';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 3) {
$image = 'archer';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
?>
With that, the class seems to work fine. but with regard to the variables top_pos -- it doesnt.
I suspect your $top_pos and $left_pos variables are not being set or being overwritten somewhere.
<?php
function getUserChar($input) {
return $input;
}
// I suspect you have not set these anywhere or are being over written somewhere
$top_pos = 123;
$left_pos = 321;
$image = '';
$_SESSION['user_login'] = 1;
$input = getUserChar($_SESSION['user_login']);
if ($input == 1) {
$image = 'warrior';
} elseif ($input == 2) {
$image = 'mage';
} elseif ($input == 3) {
$image = 'archer';
}
echo '<div class="'.$image.'" style="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
http://sandbox.onlinephpfunctions.com/code/184e6935fa39e733c145441c6a24be6651f41c25
im quite new to PHP and im trying to make a pagination, the code works! but i need to click on the button twice to make the page refreshed/change. how do i fix this?
view/main/user/userlist.php
<?php
$_SESSION['pagelim'] = 10;
$_SESSION['page'] = $_GET['halaman'];
if ($_SESSION['page'] == '') {
$_SESSION['pos'] = 0;
$_SESSION['page'] = 1;
} else {
$_SESSION['pos'] = ($_SESSION['page']- 1) * $_SESSION['pagelim'];
}
$itemcount = listpetugas::getlisted();
$pagecount = ceil(listpetugas::getlisted()/$_SESSION['pagelim']);
for ($i=1;$i<=$pagecount;$i++)
{ ?>
<?php
if ($i!=$pagecount){
echo " <ul class='pagination'><li><a href='?controller=main&action=userlist&halaman=$i' onclick='myFunction()'>$i</a></li></ul>";?>
<?php }
else{
echo "$i";
} ?>
<?php } ?>
model/userdb.php
public static function listemup()
{
if ($_SESSION['pos'] =='' && $_SESSION['pagelim'])
{
$pos = 0;
$lim = 10;
}
else {
$pos = $_SESSION['pos'];
$lim = $_SESSION['pagelim'];
}
$list = [];
$db = FirstFire::StartConnection();
$req = $db->query("SELECT * FROM randomity LIMIT $pos,$lim");
$rowcount = $req->rowCount();
foreach ($req->fetchAll() as $post) {
$list[] = new listpetugas($post['name'],$post['email'],$post['adress'],$post['wow']);
}
return $list;
}
JS
<script>
function myFunction() {
location.reload();
}
</script>
sorry for the messy code.
Hello I want to change the content on the amount of posts so if the foreach loop got only 2 posts it should say 2. But it is not working?
<?php if (!empty($iconMenu)) { ?>
<nav class="menu-top">
<?php $i = 0;
foreach($iconMenu as $icon) {
$page = Website::getActiveTreeBranch($icon['link']);
if ($page !== false) {
$i++;
if($i=1){ ?>
1
<?php } ?>
<?php if($i=2){ ?>
2
<?php } ?>
<?php }} ?>
<?php } ?>
</nav>
Simple Typo:
= is assignment operator and == is equality check operator.
Change code:
if($i=1){ ?>
1
<?php } ?>
<?php if($i=2){ ?>
To
if ($i == 1) {
echo '1'
}
if($i == 2) {
So, the final (cleaned up code):
<?php
if (! empty($iconMenu)) {
echo '<nav class="menu-top">';
$i = 0;
foreach ($iconMenu as $icon) {
$page = Website::getActiveTreeBranch($icon['link']);
if ($page !== false) {
$i++;
if ($i == 1) {
echo '1';
}
if ($i == 2) {
echo '2 ';
}
}
}
}
echo '</nav>';
?>
This answer is not for the question but to help him how to use the php tag.
<?php $i = 0;
foreach($iconMenu as $icon) {
$page = Website::getActiveTreeBranch($icon['link']);
if ($page !== false) {
$i++;
if($i==1){
Echo "1";
}
if($i==2){
echo "2";
}
}}
} ?>
</nav>
I have not double checked if there is a correct number of { and } as I'm typing on my phone.
EDIT: I forgot to edit the thing that was wrong if =
I fixed it, I had to use Count instead.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php function variable scope
I am using below code to test with a global variable. It seems that a global variable cannot be compared inside a function.
Why is it not displaying 'hello world' in the output?
Below is the code that I am trying:
<?php
$bool = 1;
function boo() {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
When I remove function boo(), 'hello world' is displayed. Why is it not displaying when function exists?
use global $var to access your variable
<?php
$bool = 1;
function boo() {
global $bool;
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
boo();
?>
Or a safer way using pointers would be to
function boo(&$bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
Looks like homework, still:
<?php
$bool = 1;
boo();
function boo() {
global $bool;
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
Or
<?php
$bool = 1;
boo(&$bool);
function boo(&$bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
Call you function, and pass $bool as a parameter and return the value.
$bool = 1;
$bool = boo($bool);
function boo($bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
return $bool;
}
use this way
$bool = 1;
function boo($bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
boo($bool);