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

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";

Related

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

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

what this line of code mean ? $channel =<<<_XML_; [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
$channel =<<<_XML_;
what is meaning of above statement ?
Is XML predefined variable ?
What is the meaning of <<<
This syntax is called a heredoc. It's very convenient to enter long (usually multiline) strings without having to mess around with escaping etc.

PHP VSW <<< (Weird Syntax with Three carets) [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I've stumbled across some code that looks like it's alternate syntax for assigning something into a variable in PHP.
$VSWTags = <<<VSW
BUNCHOFHTMLANDSTUFFHERE
VSW;
This is in a Drupal site we have here at work, and I searched on PHP.net for the VSW and for the <<< I couldn't find anything. When I played with it in the console I saw that it basically is just support for bare words or something of that nature?
php > $s = <<<VSW <<< > I dont know what this is doing
<<< > but its cool
<<< > VSW;
php > print $s;
I dont know what this is doing
but its cool
php >
Does anyone know where the documentation is for this or what it's called?
This is HEREDOC - very usefull for large strings.

What is <?=$varname?> syntax in PHP code? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
Previous I bumped into codes like this:
<?php
$var="hello";
?>
<?=$var?>
It simply prints out the content of $var, so... is the syntax equivalent to echo $var?
I'll also appreciate an answer pointing to a related manual page. Since the syntax is not searchable.
Yes, <?=$var?> is the same as <?php echo $var; ?>
From PHP.net manual: echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
You can read more here.

PHP string operations [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why is the php string concatenation operator a dot (.)?
I've always wondered -
Why does PHP use the . operator to concat strings instead of the + sign?
Is this some sort of way to improve script-evaluation performance?
Thanks
Because otherwise, what would happen in this case?
$str = "4";
$num = 2;
$result = $str + $num;
What if you wanted the result to be "42"?
Clarification
The above answers the question "why is there an operator . in addition to the operator +?". If the intended question was "why does operator + not perform string concatenation?" (with the understanding that the would need to be another operator to take over the current behavior +), then I 'll be happy to remove my answer in favor of a more relevant one.
After deciding that it (PHP) would do lots of autoboxing there was pretty much no other choice then to use 2 different operators for "adding" and "concating".
"+" for adding is obvious and #Gumbo explained why "." was chosen.
var_dump("12ab" + "34cd"); // 46
var_dump("12ab" . "34cd"); // "12ab34cd"
so you need to tell the language that you want it to do because it can do both :)
Other languages don't have that problem because they don't allow the implicit conversion from a string to an integer.
So if you write "4" + 2 the language would tell you that it can't to that and you'd need to write: intval("4") + 2 and it knows what to do.
Also see here
why-is-the-php-string-concatenation-operator-a-dot
Because Perl used the . for string concatenation and PHP was highly influenced by Perl’s syntax.

Categories