Please Explain meaning of {} syntax for me [duplicate] - php

This question already has answers here:
Whats the difference between {$var} and $var?
(5 answers)
Closed 5 years ago.
I am a newbie and try to learn PHP especially INSERT INTO statement. I'm know that if we insert value for String we must use '' syntax. But i don't understand meaning of {} syntax for "title" and"link". Any guys can explain for me. Thanks a lot.

On Sql side there is no difference between '$title' or '{$title}' . but in php {$title} is a more complete syntax of $title, that allows one to use :
"this is post {$title}s"
"{$object->data}"
"{$array['data']}"
"{$array['data']->obj->plop['test']}"
The curly braces "escape" the PHP variable and are not passed to MySQL. With a simple variable like $title it doesn't make a difference but with something like $post['title'] it does. for more information check this

Related

PHP param type hinted with '?' [duplicate]

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Closed 3 years ago.
I was reading through some of the Zend framework's hydration source code and came across something I haven't encountered before:
public function hydrate(string $name, ?array $data = null) : string
I've never seen something type hinted like the $data parameter - ?array
I don't know what to search for, so finding documentation on the syntax used there is really difficult. Does anyone know what it is called when you type hint with a question mark like that, and possibly what it does?
Even if you only point me at the right documentation I'd really appreciate any help.
It declares $data as nullable, as described in the docs

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 INSERT string with single quote ' symbol [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 6 years ago.
I want to do an INSERT into a MySQL database using:
$sql = "INSERT INTO table (title1) VALUES ('$myVar')";
but the problem is $myVar can contain the single quotes (' symbols, e.g. in "idiot's"). Can somebody tell me how to handle any single quotes in the variable as a letter and not as a piece of code?
(I know there are posts about this in the forum already, but I do not really understand their solutions, so sorry for double posting)
You might be temped to replace each single quote with two of them.
like so
$myvar = "idiot\'s";
But resist the urge and escape it instead:
<?php $var = "Hello !! idiot's";
mysql_real_escape_string($var);?>
Or even better, use PDO
Use mysqli_real_escape_string like this:
$myVar= mysqli_real_escape_string($link,$myVar);
and then your query.
It is advisable to use PDO too!

Converting from a SQL Syntax to MongoDb Syntax Issues [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I have been doing my Homework regarding mongodb and PHP and honestly, I'm fairly new at this and this is my first post at SO.
What does"." operator do in PHP?
For example
$cmd = "SELECT m_time,m_latency,m_length FROM pkt_tbl WHERE m_in_port=". $in_port
What does "." in .$in_port mean? How can I convert the entire syntax to mongodb?
This is my try:
$db->pkt_tbl->find(array("m_in_port=".$inport,array("m_time"=>1,"m_latency"=>1,"m_length"=>1));
Please correct my syntax and enlighten me regarding "." operator, I badly want to learn and I'm a newbie at PHP and mongodb.
That is incorrect PHP. Try:
$db->pkt_tbl->find(
array("m_in_port"=>$inport),
array("m_time"=>1,"m_latency"=>1,"m_length"=>1)
);
In PHP, . is the concatenation operator. It tells the interpreter to stick strings or variables together end-to-end. For example, "hello " . "world" is equivalent to "hello world". In the case of your example, if $in_port=10, then your line of code would be equivalent to "SELECT m_time,m_latency,m_length FROM pkt_tbl WHERE m_in_port=10";

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