how can i add " to a variable in php? - php

so I'm trying to make a variable that contains a " sign
like if i had a variable called $x
i want my variable
$y = """ , $x , """
but i can't just write """ so what is the method of adding a " to a variable
im using php 5 but i don't think that's gonna help i mean im justwriting more because this site doesn't allow me to post but i can't explain more and i cant find my answer in another place
and again more writing yada yada , some questions are small so WHY just WHY does it think it's incomplete

You can do it like this
$x =11;
$y = '"""'.$x.'"""';
echo $y;
Result
"""11"""

now i finally figured out what i was doing wrong , i was using , instead of . between the values so the final code is
$y = '"' . $x . '"' ;

Related

How Do I Count Up By Two In PHP? [duplicate]

This question already has answers here:
Adding 2 to a variable each time
(5 answers)
Closed 1 year ago.
So I'm trying to make it so the computer counts up to ten by two and making a new line each time. When I tested this my entire computer crashed. What's wrong with it?
<?php
$test = 0;
while ($test < 10) {
$test + 2;
echo $test . "/n";
}
?>
Change:
$test + 2;
to:
$test += 2;
Currently, $test is always 0 in your code, since you are not assigning a new value to it, hence the infinite loop.
Additionally, you would also want to change:
echo $test . "/n";
to:
echo $test . "\n";
Since you need to use a backslash to indicate a new line character.
N.B. In PHP you can also use the PHP_EOL constant to indicate the end of a line.
Here you just sum $test with 2 but did not assign it back to $test
$test + 2;
Trying replace it by $test += 2;
Another point is /n is not "making a new line" as you expected, change it to \n instead.
Although you can use while statement, this is a good case to use for instead.
for($i=0; $i < 10; $i += 2)
{
echo $i . PHP_EOL;
}
Usually while is used to evaluate something every loop, some variable that can change inside the while, like a bool variable or an "infinite" loop until break.
When you have a already defined number of loops, for is usually a better approach.

How to use 2 variables in For loop? [duplicate]

This question already has answers here:
Php for loop with 2 variables?
(7 answers)
Closed 6 years ago.
My PHP code (so far not working as I want it to) -
for ($x=0, $y=0; $x<=163, $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
What I was trying to achieve is - it should echo even if one condition matches. Right now it stops when one condition is fulfilled, i.e. in this case when y=98, it stopped. Also there may be cases when y>=x in contrast to given code where x>y
Edit -
The duplicate marked question, did not solve my problem as in that question, range of both the variables were same, so it could have been achieved by the mentioned answer (by increasing one variable). In my case both variables has different range.
Also I tried this
for ($x=0, $y=0; $x<=163 || $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
But it's also not helping me to achieve my desired output.
Edit 2 - I think I couldn't explain properly earlier, with what I wanted the output to be.
So I am trying to demonstrate by small e.g. with x=3, y=2
x=1, y=1
x=2, y=1
x=3, y=1
x=1, y=2
x=2, y=2
x=3, y=2
I am trying to achieve something like this. I don't know, what this ?matrix is called in maths (so it's possible my question title is wrong).
You can achieve that through a basic nested loop. Basically you loop over y in the outer loop and for each iteration of y you do a another for loop for x. Try this and adjust x and y as necessary.
<?php
for ($y=0; $y<=98 ; $y++) {
for ($x=0; $x<=163; $x++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
}

PHP code snippet decode

I was looking a craigslist for web dev jobs (I am a beginner).
I came across a Jr. job that wanted me to decode this snippet.
<?php
$f1 = 'e' . 'x' . 'p';
$f1 .= 'l' . 'o' . 'd' . 'e';
list($f2,$ext) = $f1('.',$argv[0]);
$x = array('a','H','I',
'r','Y','2',
'x','q','c',
'm','R','l',
'd','k','B',
'l','d','m',
'9','r','b',
'm','93','L',
'm','N','v',
'b','Q','');
echo $f2(implode('', $x)) . "\n";
?>
Since I do not know the answer I a a bad candidate for the job, but I would like to understand it if anyone can help me. What I do get is that you are using the string of $f1 to manipulate the array $x. But i see problems with using $argv[0] and I believe that using f2(implode('', $x)) will give a parameter error.
Any help would be greatly appreciated.
Hehe, that was kidna fun. It's the email to their HR department.
<?php
$argv[0] = 'base64_decode.';
$f1 = 'explode';
list($f2,$ext) = $f1('.',$argv[0]);
$x = array('a','H','I',
'r','Y','2',
'x','q','c',
'm','R','l',
'd','k','B',
'l','d','m',
'9','r','b',
'm','93','L',
'm','N','v',
'b','Q','');
echo $f2(implode('', $x)) . "\n";
Not really sure what they were looking for in an answer. Made me think of this though. http://blog.sucuri.net/2013/09/ask-sucuri-non-alphanumeric-backdoors.html

Echo statement not coming out as i want it [duplicate]

This question already has answers here:
Not getting the specific output without braces [closed]
(3 answers)
Closed 8 years ago.
This code only prints out '17' instead of "10 + 7 = 17". Why is this? And how can i solve this.
<?
$x = 10;
$y = 7;
echo $x . '+' . $y . '=' . $x+$y;
?>
What you should understand is that echo won't do anything until the result of the whole expression given to it is evaluated. This expression contains both . and + operators - the first one concatenates its operands (glues two strings together), the second adds numbers.
Now, you might think that + operator has a higher precedence than of . one - in other words, that result of $x + $y will be calculated before (eventually) glued to the rest of the string. But it's not. So we can say this statement is actually treated as...
echo ($x . '+' . $y . '=' . $x) + $y;
In other words, the result of all the string concatenation is added (converted to number) to $y, and only the result of this operation is printed.
The result of $x . '+' . $y . '=' . $x is '10+7=10', and it doesn't look like something that might be added. But guess what, PHP wants to be nice to you - and it assumes that you actually wanted to extract the first digits of a string when you tried to convert it to a number. So the whole line is treated as number 10. When added to 7, it's just 17 - that's why you got 17 echoed.
One possible workaround is getting rid of ., using , operator instead (as its precedence is lower - in fact, it's the lowest among operators):
echo $x, '+', $y, '=', $x + $y;
It could be simplified if one remember about such a handy feature of PHP as string interpolation:
echo "$x + $y = ", $x + $y;
<?php
$x = 10;
$y = 7;
function sum($a,$b){
return $a+$b;
}
echo $x.'+'.$y.'='.sum($x,$y);
?>

Make an equation in php

I have a php file where i want to make an equation with the output i get.
It is pretty easy if the line is like this:
<?php echo round(($post['voteup']/($post['voteup'] + $post['votedown'] ))*100); ?>
But i have a php line where it goes like this, and i just can't figure out how to make the equation work:
$str='';
$data = $dbc->query($sql);
if($data!=null && $data->num_rows>0){
while( $row = $data->fetch_array(MYSQLI_ASSOC)){
$str.="<p>".$row['voteup']." + ".$row['votedown'].</p>";
}
It may be a silly question, but this really bothers me.
If you want to add the upvotes and downvotes you would do this -
$newTotal = $row['voteup'] + $row['votedown'];
$str.="<p>" . $newTotal ."</p>";
If you are trying to add $row['voteup'] and $row['votedown'] together you should addition them in a variable first then use it.
$total = $row['voteup'] + $row['votedown'];
$str .= "<p>{$total}</p>";
If you really need to make it inline you can do something like this as well.
$str .= '<p>' . ($row['voteup'] + $row['votedown']) . '</p>';
If you just want to display the "equation" simply fix the error.
$str.="<p>".$row['voteup']." + ".$row['votedown']."</p>";
Or you could actually turn it into an equation:
$str.="<p>".$row['voteup']." + ".$row['votedown']." = ".($row['voteup'] + $row['votedown'])."</p>";

Categories