Multiple statements in Ternary Operator [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert a small condition in php to ternary operator.
if($i==2) {
$third="third";
$i=0;
} else {
$third="";
}
How to Translate this to ternary operator?
Thanks.

You can use like this format:
$i==2 ? ($third = "third" AND $i = 0) : ($third="");

Because i'm bored:
list($i, $third) = array(($not2 = ($i != 2)) * $i, ($not2) ? '' : 'third');
or
list($i, $third) = ($i == 2) ? array(0, 'third') : array($i, '');
or maybe
$third = ($i == 2 ? "third" : '') and $i = 0;
or
$i *= !($third = ($i == 2) ? 'third' : '');
But don't do this in real life. Seriously. You have a perfectly readable if; there's no good reason to turn it into a mess like this.

Related

How to add to a conditional variable for comparison [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
At this point in time, the below variables are set at the values shown and I need to compare:
(Pseudo code)
if $foo is > 0 AND that $i == $bar +1.
This is just a simplified example. In my code these variables are different at various times.
I'm not having any success figuring out how to do this comparison. Any help is appreciated.
$i = 4;
$foo = 4;
$bar = 3;
if($foo > 0 && ($i == ($bar + 1))) {
echo 'do something';
}
Thanks in advance for any help pointing me in the right direction.
Honestly think your code should work (after the parentheses fixes), but whatever your real logic is instead of the example code you gave, you can try wrapping all the logic areas.
$i = 4;
$foo = 4;
$bar = 3;
if(($foo > 0) && ($i == ($bar + 1))) {
echo 'do something';
}

convert C to PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I found this C source here. But needed it in PHP. Don't know how to code. Simply asking for help.
#include <stdio.h>
#define LENGTH 3
void print_binary(int n)
{
int bit = 1<<LENGTH - 1;
while ( bit ) {
printf("%d", n & bit ? 1 : 0);
bit >>= 1;
}
printf("\n");
}
int main(){
int n = 1<<LENGTH, i;
for(i=0;i<n;i++)
print_binary(i);
}
Here is a simple conversion.
<?php
define('LENGTH', 3);
function print_binary($n)
{
$bit = 1<<LENGTH - 1;
while($bit)
{
echo $n & $bit ? 1 : 0;
$bit >>= 1;
}
echo "\n";
}
$n = 1<<LENGTH;
for($i = 0; $i < $n; $i++)
print_binary($i);
?>

Mathematical operation of CHARACTER [not numeric] in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
is there any way to sum/substrate character in php?
For example if
$var1 = 'a';
$var2 = 'b';
$var3 = 'a';
$calculation = $var1 - $var2 + $var3;
echo $calculation;
I want the output as 2a-b
Just like we did in high school algebra?
I wrote a simple function to make something like what you want.
It's just an example, you will have to improve it a lot if you really want to use it, but is a good start.
Limitations:
Only works with letters (Won't work propely if you add numbers, you will have to add that functionaliy).
ALL the letters must have their plus or minus.
You must use spaces before a plus or minus.
This is definitely not the best way to do it, as I said you have to improve it. I wrote it fast but I tested it a bit.
<?
function calc($str){
$data = preg_split("/ /", $str);
$used = Array();
$buffer = "";
foreach ($data as $pos=>$letter){
foreach ($data as $pos2=>$letter2){
if ($letter[1] == $letter2[1] && !in_array($pos, $used) && !in_array($pos2, $used) && $pos != $pos2){
$first = $letter[0] == '+' ? 1 : -1;
$second = $letter2[0] == '+' ? 1 : -1;
$buffer .= ($first+$second).$letter[1];
$used[count($used)] = $pos;
$used[count($used)] = $pos2;
}
}
}
foreach ($data as $pos=>$letter){
if (!in_array($pos, $used)){
$buffer .= $letter;
}
}
return $buffer;
}
echo calc("+a -b +a");
?>
Output:
2a-b

Find first zero bit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What's the easiest way to find the first zero bit with PHP?
For example, say I have an integer 47 which is 111101, how can I find out that the 5th bit is the first unset bit? This needs to work to cater for different integers.
$value = 47;
$i = $j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
$i++;
}
echo "bit $i is 0";
If you want to eliminate the use of $i as a counter, you can do a little bit of extra math:
$value = 47;
$j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
}
echo "bit ", (log($j) / log(2) + 1), " is 0", PHP_EOL;
The +1 is necessary because you're starting your binary as bit 1 rather than as bit 0
Use decbin to return a string of 0 and 1.
Then, use strpos to find the first 0 caracters.
$str = decbin(47);
$result = strpos($str, '0');

Separating numbers processed with a foreach conditional into specific category of numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is a scenario: The $numbers array in the code below contains one thousand numbers(0 - 1000). I need to count how many of these numbers fit into each of the following categories:
how many of them are between 1 and 1000 inclusive,
how many of them are less than 1, and
how many of them are greater than 1000.
I have created a foreach loop to look at each number one after the other, but right now it's treating every number like it belongs in all three categories.
How do I get the counts correct? ie., how many numbers fit into the "Less than 1", "Between 1 and 1000", and "Greater than 1000" categories, respectively.
The current code:
$numbers = get_numbers();
$count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
$count_less_than_one += 1;
$count_between_one_and_thousand += 1;
$count_greater_than_thousand += 1;
}
Very simply just include the conditions. You can just use if
foreach ($numbers as $number) {
if ($number < 1) $count_less_than_one += 1;
else if ($number >= 1 && $number <= 1000) $count_between_one_and_thousand += 1;
else $count_greater_than_thousand += 1;
}

Categories