I've got 2 vars - $j and $r
In string "$jx$r" php sees $jx as variable, but "x" is a string.
You need to reformat your string a litte:
Tis might be easier to read:
echo $j."x".$r
But if you want it in one string:
echo "{$j}x$r";
See the manual page for double quoted strings, there is also an explaination how the curly braces work.
Related
There is a tag:
<br />
Ok, but why double quotes and dot before first quote are needed with br in php, when in HTML there is only <br />? :)
For example:
echo $variable1."<br />";
echo $variable2;
That's an echo statement with a concatenated string. You can use just a regular old <br>, but you have to put it as HTML outside of the PHP tags (<?php ?>).
These are all valid in a PHP file:
<?php ?>
<br>
<!--This one is specifically to show that a regular br has to be
outside of the php tags.-->
<?php
echo "<br>";
?>
<?php
echo $var."<br>";
?>
Double Quote
Double quote strings will display a host of escaped characters
(including some regexes), and variables in the strings will be
evaluated. An important point here is that you can use curly braces
to isolate the name of the variable you want evaluated. For example
let's say you have the variable $type and you want to echo "The
$types are". That will look for the variable $types. To get around
this use echo "The {$type}s are" You can put the left brace before
or after the dollar sign. Take a look at string parsing to see
how to use array variables and such.
Dot
Two string operators are available. The first is the concatenation
operator ('.') that returns its right and left argument
concatenation. Second is the concatenation operator of assignment
('.='), which adds the right-hand argument to the left-hand
argument. For further details, please read Assignment
Operators.
I have an existing regex which checks if the string is not wrapped in either quotes or square brackets then
I'm wrapping that string in quotes
My existing regex is as follow -
if (!preg_match('/^["\[].*["\]]$/', $filter)) {
$filter = '%22' . $filter . '%22';
}
Now I want to extend this regex to check already wrapped in either quotes or square brackets or parentheses
For parentheses, my string value i.e my $filter value would be something like (123 456)
Can anyone help to extended this regex?
I think the regular expression is good for complex string math but like this simple string match why do you use regex it takes almost O(n) time where n is you string size some time it takes O(2^m), where m is the length of regex. But if you check with a simple if just check 1st and the last characters of string it takes O(1). Here is the regex solution.
/^(?(?=")(["].*["])|(?=\[)([\[].*[\]]))|(?=\()(([\(].*[\)]))$/
The regular expressions are a powerful tool but they are not a Swiss army knife. There are problems that simply cannot be resolved using regex and there are problems that can be resolved using regex but a simpler approach produces code that is easier to read and understand. This is such a problem.
Let's reformulate the problem. If the first character of the string is " and also its last character is " then the string is wrapped in quotes and it does not need other processing. The same when the first character is [ and the last one is ]. Or ( and ).
The first character of a string stored in the variable $filter is $filter[0]. The last one is $filter[-1]. Extract them into a new string and search for it into a list of combinations of quotes and parentheses:
if (! in_array($filter[0].$filter[-1], ['""', '[]', '()'])) {
// the string is not enclosed in quotes, square brackets or parentheses
// do something with it (enclose it, etc)
}
If you are using PHP 5 (any version) or PHP 7.0 then you are out of luck (and out of PHP updates, btw) and you cannot use $filter[-1] (because this functionality has been introduced in PHP 7.1).
The PHP function substr() comes to the rescue.
substr($filter, -1) does the same thing as $filter[-1] (returns the last character of $filter and works in all PHP versions.
There are two corner cases to consider:
When $filter is '"' (a string of exactly one character that is a double quote), the code above will report it as enclosed in quotes when, in fact, it is not.
When $filter is '' (the empty string) the code produces two warnings (but does not report it as being enclosed in quotes.
Both cases can be easily solved by adding a check of the string's length to avoid running the other test if the string is too short:
if (strlen($filter) < 2 || ! in_array($filter[0].$filter[-1], ['""', '[]', '()'])) {
// the string is not enclosed in quotes, square brackets or parentheses
// do something with it (enclose it, etc)
}
I want to use this variable below with dot in php but server remove its dot, Is there any solution?
$file=adetails.php;
I can't change dot to another character.
Quote your strings, don't rely on magic constants.
$file=adetails.php; means $file = "adetails" . "php";. The . is "lost" because it is a concatenation operator and not part of the string.
You want $file = "adetails.php";
You seem to be assigning a value to a variable and as I get it from your question adetails.php is a string value you would like to assign to $file. If that is the case, you need to enclose that string into quotes:
$file="adetails.php";
I was wondering, how is the semantics of braces exactly defined
inside PHP? For instance, suppose we have defined:
$a = "foo";
then what are the differences among:
echo "${a}";
echo "{$a}";
that is, are there any circumstances where the placement of the
dollar sign outside the braces as opposed to within braces makes
a difference or is the result always the same (with braces used
to group just about anything)?
There are a lot of possibilities for braces (such as omitting them), and things get even more complicated when dealing with objects or arrays.
I prefer interpolation to concatenation, and I prefer to omit braces when not necessary. Sometimes, they are.
You cannot use object operators with ${} syntax. You must use {$...} when calling methods, or chaining operators (if you have only one operator such as to get a member, the braces may be omitted).
The ${} syntax can be used for variable variables:
$y = 'x';
$x = 'hello';
echo "${$y}"; //hello
The $$ syntax does not interpolate in a string, making ${} necessary for interpolation. You can also use strings (${'y'}) and even concatenate within a ${} block. However, variable variables can probably be considered a bad thing.
For arrays, either will work ${foo['bar']} vs. {$foo['bar']}. I prefer just $foo[bar] (for interpolation only -- outside of a string bar will be treated as a constant in that context).
The brackets delimit where the variable name ends; this example should speak for itself.
$a = "hi!"
echo "$afoo"; //$afoo is undefined
echo "${a}foo"; //hi!foo
echo "{$a}foo"; //hi!foo
Also, this should spit out a warning; you should use
${'a'}
Otherwise it will attempt to assume a is a constant.
Also you can use braces to get Char in the position $i of string $text:
$i=2;
$text="safi";
echo $text{$i}; // f
I was going through a codebase and came across a line I had a question about. It's something I haven't seen before and I was wondering if someone could explain it for me. Here's the code:
$variableName = $array[1];
$variableName{0} = strtolower($variableName{0});
$this->property = $variableName;
What are the curly braces being used for? I've used curly braces to define variables as variable names before, but is this the same thing? I can't seem to find any resources online that explain it, but I'm not sure if I'm searching for the right thing.
access the single byte with that index {0} => first char (in non-utf8 string)
you could simply test it with:
$var='hello';
echo $var{0};
It's setting the first character of the string to lower case. It's a string shortcut operator, functioning the same as this:
<?php
$variableName = strtolower(substr($variableName, 0, 1)) . substr($variableName, 1)
Curly braces {} work the same as square brackets [], for array or string indexing. I'm guessing it is borrowed from perl, in which the square brackets are used for arrays and braces are used for hashes. But in PHP arrays and hashes are the same thing.