Beginner if statement help - php

if ($row['active'] == 1) echo ''.htmlspecialchars($row['username']).''; else echo htmlspecialchars($row['username']);
Could I write this shorter and cleaner somehow?

echo $row['active'] == 1 ? ''.htmlspecialchars($row['username']).'' : htmlspecialchars($row['username']);
explained a little here http://www.addedbytes.com/php/ternary-conditionals/

I'm assuming you made a mistake putting the $id in a single quoted string, and meant for php to put the value of $id in its place in there.
$name=htmlspecialchars($row['username']);
if($row['active'] == 1) {
echo "<a href='prof?id=$id'>$name</a>";
} else {
echo $name;
}

You could take advantage of the ternary operator:
echo ($row['active'] == 1)
? ''.htmlspecialchars($row['username']).''
: htmlspecialchars($row['username'])
;
(I split the code onto separate lines for the sake of formatting.

Related

Checking if two variables are not empty in PHP

I want to echo variables if either of them aren't empty.
I have written the following PHP but they aren't being shown when the variables aren't empty:
if(!empty($this->element->videos||$this->element->productdownloads)) {
echo $this->element->videos;
echo $this->element->productdownloads;
}
When checking with the OR operator (||) the code will execute if one or none of them is empty. But you echo both variables even if one of them is empty.
What I think you want to do is use the AND operator(&&). This way, the code will only execute if none of the variables are empty.
<?php if(!empty($this->element->videos) && !empty($this->element->productdownloads)) {
echo $this->element->videos;
echo $this->element->productdownloads; }
?>
if you still want to show videos, even if productdownloads is empty (and vice versa), you could do a seperate if for each of them, like this:
if(!empty($this->element->videos){
echo $this->element->videos;
}
if(!empty($this->element->productdownloads){
echo $this->element->productdownloads;
}
edit: minor grammatical fixes
<?php
if(!empty($this->element->videos) || !empty($this->element->productdownloads)) {
echo $this->element->videos;
echo $this->element->productdownloads;
}
?>
if(!$this->element->videos || !$this->element->productdownloads)
{
echo $this->element->videos;
echo $this->element->productdownloads;
}
You dont need to use empty by default php checks for empty when you use ! sign with if condition
Try
if(!($this->element->videos && $this->element->productdownloads)){
echo $this->element->videos;
echo $this->element->productdownloads;
}
Well, I have had quite number of issues like that.
Try to remove empty space from the two variables before comparing them. Sometimes empty spaces comes before the variable and that hinders comparison.

php assistance with quiz results

I have a module that the user clicks through and watches a video. There are progress checks throughout that are not graded but the requester now wants the user to have to retake the progress check if they didn't get all of the answers correct. I believe the line of code for this needs to be in the next button back to progress check. Thank you for any help!
Here is my code:
progress results.php
if ($feedback = "Correct!") {
echo '<a id="next" href="' . $filename . '.php?page=' . ($page) . '">Next</a>';
}
else {
echo '<a id="next" href="progress_check.php?page=' . ($page+1) . '">Next</a>';
}
In PHP, we have three kinds of "=" uses :
Assignment operator : =
$var = 'Alex';
Here, we just asign the value of Alex to a variable called $var, we do not make any other type of operation as comparing values.
Comparison operator : Equal to ==
$var == 'Alex';
Here, we check if the variable $var has a value equal to Alex. Frequently used in an if statement to compare two variables.
if ($var == 'Alex') {
# code...
}
Comparison operator : Identical to ===
And here, we make sure if the variables have the same values and an identical type, for example comparing a string and an integer, like this.
$var = '5';
if ($var === 5) {
# code...
}
As you see, we are using an if statement to check if either has the same type, not just the same value, in this case, the result is false.
Your case
Try this :
if ($feedback == "Correct!") {
echo '<a id="next" href="' . $filename . '.php?page=' . ($page) . '">Next</a>';
} else {
echo '<a id="next" href="progress_check.php?page=' . ($page+1) . '">Next</a>';
}
More information about this topic right here : Comparison Operators
I hope that help you to understand the behavior.

if statement syntax equal to

I am using Views php in Drupal 6. I need to do an if statement to find if nodehierarchy_parent is equal to the value 0. Then do something. Right now I have it set to if the value contains a 0. My code is below.
<?php
if (strpos($data->nodehierarchy_parent,'0')) {
print 'hello';
}
else print $data->nodehierarchy_parent;
?>
I am new to stack. John's comment solved my problem but I cant mark his comment as correct answer. Below is the final code that worked.
<?php
if ($data->nodehierarchy_parent == 0) {
print 'hello';
}
else print $data->nodehierarchy_parent;
?>
echo (strpos($data->nodehierarchy_parent,'0') === false)
? $data->nodehierarchy_parent
: 'hello';

Using PHP if condition inside HTML code

I am using HTML inside PHP like this.
for($i=0;$i<count($arrSql);$i++)
$opt.="<option". if($_GET['pId'] == $arrSql[$i][0]){ echo "Selected";} ."value=".$arrSql[$i][0].">".$arrSql[$i][1]."</option>";
I have tested it for a long time and it looks correct, but it is showing an error and I don't know where the bug is.
You can't use concatenation and if together. Change it to:
for ($i=0;$i<count($arrSql);$i++) {
$opt .= "<option"
. ($_GET['pId'] == $arrSql[$i][0] ? " selected" : '')
."value=".$arrSql[$i][0].">".$arrSql[$i][1]."</option>";
You forgot the spaces.
Try following:
for($i=0;$i<count($arrSql);$i++)
$opt.="<option". ($_GET['pId'] == $arrSql[$i][0] ? ' selected="selected" ' : ' ') ."value=".$arrSql[$i][0].">".$arrSql[$i][1]."</option>";
Your echo call is not needed in this place. Your statement only concatenates strings and does not print them.
Additionaly it's not possible to use an if statement inside a string concatenation. However the if shortcut, the so called ternary operator is applicable in this situation.
And as pointed out in an other answer there is also a space missing before the selected part.
for($i=0;$i<count($arrSql);$i++) {
$opt .= "<option "
.($_GET['pId'] == $arrSql[$i][0]) ? "Selected" : ""
."value=" .$arrSql[$i][0]
. ">" .$arrSql[$i][1]. "</option>";
}
An alternative using if that might be more clear is:
for($i=0;$i<count($arrSql);$i++){
$opt .="<option ";
if ($_GET['pId'] == $arrSql[$i][0]){
$opt .= "Selected";
}
$opt .= "value=" .$arrSql[$i][0]. ">" .$arrSql[$i][1]. "</option>";
}
If you want to, you can even inline the array accesses into the string by using curly braces leading to this last line: (more here)
$opt .= "value=${$arrSql[$i][0]}>${$arrSql[$i][1]}</option>";
For the future you might want to enable error output in your scripts. This would have indicated the main error.

User-friendly boolean printing in PHP

In the below code 'resAvailability' might be equal to 1 or 0. Is it possible to update this code in such a way that 1 produces 'Yes' and 0 results in 'No'?
<?php foreach ($result as $row):?>
<tr>
<td><?php echo $row['resAvailability']; ?></td>
<?php endforeach;?>
echo $row['resAvailability'] ? 'Yes' : 'No';
This is called the ternary operator.
This is the way I would do it:
echo ($row['resAvailability'] == 1) ? "Yes": "No";
Be aware that 1 will also validate as true and 0 as false so in actual fact you don't need the == 1 in my example as either way it will run as:
Is $row['resAvailability'] true, return yes, else return no.
You mean like this? Very basic stuff
if($row['resAvailability'] == 1)
{
echo "Yes";
}
else
{
echo "No";
}
edit
Emil his code is effectively the same is this, though since you asked such a basic question I thought you were quite new and in my opinion this is easier for beginners ;) though I would definitly go with Emil's way (less code and all that).

Categories