How Do I Insert a Variable into a PHP 'Include'? - php

Not much to explain really, I just cant figure out the correct syntax to use a variable in an include URL
heres what I thought it would be:
$round_num=1;
include '../../fixtures/round{$round_num}fix.php';
It returns the usual:
Warning: include(../../fixtures/round{$round_num}fix.php) [function.include]: failed to open stream: No such file or directory
This must be a very simple one for allot of you out there lol but i just cant find my answer ANYWHERE!

Use double quotes (") to delimit your string. String interpolation doesn't occur in single quoted (') strings.
$round_num=1;
include "../../fixtures/round{$round_num}fix.php";

you can also use alternate syntax such as:
include "../../fixtures/round" . $round_num . "fix.php";
Sometimes people prefer to have the variable highlighted by their IDE for clarity.

Related

Trying to use a $ value inside a path to move the files based on this php file

I am trying to get the file to upload an image or audio file to the following directory
$dir='sites/default/documents/"$patientID"/' but I don't know how to use the exact syntax with the $ sign inside a path.
I used the get method to get the patientID from the controller but still nothing is working
Single quotes do not interpolate values, even if there are double quotes inside them. The initial string must be a double quote string, in order for any interpolation to occur inside of it.
If there's any doubt about the complexity of the variable name, use braces to encapsulate the interpolation. $dir="sites/default/documents/{$patientID}/";
Use this instead : $dir="sites/default/documents/$patientID/";
In your case the variable is considered as a text.

Put a variable in a string PHP

I am new to Laravel and I am having this question.
I tried out this line of code and it works fine: return redirect("/cards/{$note->id}");
But when ever I try to use the single quotes, it does not work: return redirect('/cards/{$note->id}');
How can I solve this problem ?
What you are doing first is called variable interpolation or string interpolation. You can read more about it here, on PHP docs and here, on Wiki.
It's a feature in PHP that allows you to pass a string and have variables/placeholders inside interpreted.
In your second example you are using single quotes, which does not provide this feature, so you will have to break it up and add the variable manually to the string:
return redirect('/cards/' . $note->id);
If you are interested in a more elaborate explanation and the performance behind it then you can read more on this answer here by Blizz
He concludes that:
Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.
However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.
You should use "/cards/{$note->id}" or '/cards/'.$note->id
The most important feature of double-quoted strings is the fact that variable names will be expanded.
When a string is specified in double quotes or with heredoc, variables are parsed within it.
From PHP documentation
Use it like that:
return redirect('/cards/'. $note->id);
With either single or double quotes

PHP string concat without the "dot" operator

I'm working to integrate a plug-in into a PHP web application, and one line of the code puzzles me:
$sql = "update inventory set qtyleft='$qtyleft',price='$price',sales=sales+'$sales',qtysold=qtysold+'$qtysold' where id='$id'";
mysql_query($sql);
where $qtyleft, $price, $sales, $qtysold and $id are all variables.
I'm not very familiar with PHP, but I always thought string concatenation in PHP is done by using the . operator and it seems to me that the code above is just a long string without actually putting those variables to the SQL query. Is that the case?
In PHP, double quote (") delimited strings will evaluate variables in them.
$foo = 42;
echo "The answer for everything is $foo"; // The answer for everything is 42
This specific example is very bad because you shouldn't include variables directly in an SQL query, and shouldn't use mysql_query in new code.
See more:
Why shouldn't I use mysql_* functions in PHP?
How can I prevent SQL injection in PHP?
See Variable Parsing section of the Strings manual page.
When a string is specified in double quotes or with heredoc, variables are parsed within it.
If you use single quotes for a string, the variables will not be interpolated. If you use double quotes, they will be.
The code you mentioned will work in PHP without any issues. Please refer PHP Manual for more details.
Other issue that you might need to look forward is the function mysql_query is depreciate. Please refer here. Which gives me a feeling that the plugin you are going to is use not maintained correctly. And one more problem is, its not a good practice to pass the variable directly in the SQL query do to possible security issues
Some call it "variable interpolation". It is explained on the Variable parsing section of the manual page about strings. It helps to read the entire page and also the user comments.
The basic idea is that for strings enclosed in quotes (") and on heredoc blocks, PHP searches for variables inside the string when it needs to use it and replaces them with their values at the moment of the execution. This means the same string can render to different values in different moments of the script's execution.
This is just syntactic sugar, it doesn't change the way the code behaves and any string that contains variables inside can be rewritten using the string concatenation operator (.). Usually this syntax produces shorter source code. Sometimes the code is easier to read this way, other times it is harder because the complex expressions (array access, f.e.) need to be enclosed in curly braces ({ and }) inside the string.

require path in php

I have the following code:
require("\create_form\view.php");
and receiving the following error:
Warning: require(\create_formiew.php) [function.require]: failed to open stream: Invalid argument in C:\xampp\htdocs\training\school\STU001_MAIN.php on line 67
You can see from the error message that "require(\create_formiew.php)" has a missing "\v", whereas I wrote "\create_form\view.php".
What's the problem?
Either escape the backslashes in double quotes, or just use forward slashes. Forward slashes work on Windows too.
Also you probably don't want to use an absolute path.
require("./create_form/view.php");
Or make it relative to the document root with:
require("$_SERVER[DOCUMENT_ROOT]/create_form/view.php");
// Note: Use without key quotes only in double quoted string context!
You can use 'DIRECTORY_SEPARATOR' to avoid errors with system specific file paths.
\v is the escape sequence for a vertical tab.
Either use single quotes in your strings or escape the backslashes (\\) to avoid ambiguities.
try using
require("/create_form/view.php");
Try using double (\) backslashes between directorys:
require("\create_form\view.php");

PHP can't read filename which has special character !

i have problem with file() in php. The function can't read a file start with ! or $ like !textfile.txt or $textfile.txt, i try with fopen() and it happen as a same way. I have no idea how to solve this. I hope someone can help
Appreciate any help.
The filename "$textfile.txt" will not work as expected because variable interpolation happens in double quotes as a result value of variable $textfile will be appended with .txt and the result will be used as filename. If $textfile is undefined (which mostly is the case), .txt will be used as the filename.
To fix this use single quotes around the filename as '$textfile.txt' or if you have to use double quotes, escape the $ as: "\$textfile.txt"
But I see no problem with "!textfile.txt"
echo file_get_contents("\$test.txt");
Works.
You need to escape special characters or use single quotes.

Categories