Update a URL using a variable - PHP [duplicate] - php

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 4 years ago.
I would like to assign a variable into the PHP code, which will change my URL. For example
$page = 'http://www.example.com/search-products?type=buildings&q=small&go=Go';
Where q=small i would like to change to say q=big (using a variable)
I have assigned a variable within PHP but i am unable to get it to work?
for example
$q= 'big';
$page = 'http://www.example.com/search-products?type=buildings&q=$q&go=Go';
The url does not however update - Any help would be appreciated

Use strings with " and not with ' if you're using variables in it.
$page = "http://www.example.com/search-products?type=buildings&q={$parameter}&go=Go";

Check this :
$page = "http://www.example.com/search-products?type=buildings&q={$q}&go=Go";
Note: Single quotes don't work in this case.
If you use Single quotes, you see something like this :
echo 'q={$q}';
//Output => q={$q}

Related

How Can you add a variable inside do_shortcode("") [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
PHP - concatenate or directly insert variables in string
(15 answers)
Closed 1 year ago.
So how would you add a variable inside a do_shortcode(''); Basically i have a variable holding a URL which is what I want to add inside the url attributte, but i am not sure how to do it.
This is what I tried so far
<?php echo do_shortcode('[evp_embed_video url='"'. $video .'"' preload="auto" template="mediaelement"]'); ?>
but of course, it didn't work. Any advice on how to properly format this?

Combining php code with html doesnt work inside <li> tag [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
my php code doesn't work inside a li tag.
I tried several things but it doesn't work.
echo '<li $page_title=="Cart" ? "class="nav-item active"" : "">';
I don't understand why it doesn't work, can someone help me with this code?
You can't execute php code inside of single quotes, and you can't do ternary operators inside of double quotes or single quotes. Store it in another variable and run it that way.
$class = ($page_title=='Cart') ? "class='nav-item active'" : '';
echo "<li $class>";
https://3v4l.org/uhQAi

Eval Php variable with double quotes inside [duplicate]

This question already has answers here:
How do I execute PHP that is stored in a MySQL database?
(7 answers)
Closed 4 years ago.
EDIT: This question has been edited from the original
I have a string in a database with HTML and PHP variable's inside. Some of the HTML has double quotes as if I try to use single quotes the database escapes it by adding a quote in front of it like so: ' '.
I want to query the string and assign it to variable $x. And then use eval("\$x = \"$x\";"); to parse the PHP variable, but it seems the double quote is ruining the eval(), and the variables are not parsing.
Is there a way to allow PHP to read the variable?
I am aware, but anyone reading this should also be aware that using eval() can be very dangerous!
Any help would be greatly appreciated!
If your SQL string looks like this: myVar then php:
$myVar = 'hello!';
echo $$var;
If your SQL string looks like this: 3 + 5 then php:
eval($var);
In first option we use Variable Variables
In second option we use eval to evaluate code in string.

How to extract last part in PHP? [duplicate]

This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php

php variable from variable [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

Categories