Include Constant in String Without Concatenating

Include constant in string without concatenating

No.

With Strings, there is no way for PHP to tell string data apart from constant identifiers. This goes for any of the string formats in PHP, including heredoc.

constant() is an alternative way to get hold of a constant, but a function call can't be put into a string without concatenation either.

Manual on constants in PHP

Can I create a string constant by concatenating string and int constants in C#?

No, string constants cannot be expressions that are evaluated at runtime.

  • https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#constant-expressions

The following conversions are permitted in constant expressions:

  • Identity conversions
  • Numeric conversions
  • Enumeration conversions
  • Constant expression conversions
  • Implicit and explicit reference conversions, provided that the source of the conversions is a constant expression that evaluates to the null value.

Other conversions including boxing, unboxing and implicit reference conversions of non-null values are not permitted in constant expressions.

Converting an int to a string falls in the "other conversions" which are not permitted, since this requires a call to the ToString() method.

In point of fact, the conversion requires a call to int.ToString() which is defined to use the G10 number format. This depends on the current culture's NumberFormatInfo.NegativeSign so could in principle change. I'm not aware of any default culture which uses anything for the negative sign other than the \u002d hyphen-minus character, but you could certainly set it at runtime to something else if you want to see the world burn.

Insert a PHP constant into a string

$qp = "UPDATE ".DB_NAME.".users SET palpiteatual...";

Note that, if you don't have multiple connection and multiple database, no need to use DB_NAME

Why the class constant value is getting evaluated in one statement and not in other?

Afaik this is not possible. The whole "variable parsing" (extended curly syntax) in strings is done base on variables. Variables always start with a $ sign, so not starting with a $ does not seem to work. (i.e. Everything which is not (part of) a variable cannot be used.).

Simplified example (which wont work):

const TEST = 'A & W';
echo "I'd like an {TEST}\n";

Same for function calls (which also do not work directly)

$test = '   A & W   ';
echo "I'd like an {trim($test)}\n";

Only in "sub" curly braces the desired output can be used, but it has to be parsed as a variable again which makes it impossible (at this point).

Does work:

$AW = 'A & W';
$test = ' AW ';
echo "I'd like an {${trim($test)}}\n";

Edit:

If you truly WANT to output a (class) constant inside a complex curly brace expression:

class beers {
const softdrink = 'rootbeer';
}

function foobar($test) {
$GLOBALS[$test] = $test;
return $test;
}

echo "I'd like an {${foobar(beers::softdrink)}}\n";

This is far from what I would recommend to do!!!

PHP - calling a constant from inside a string

Not possible to do that by default. It might be possible to write a function to parse the string, but why would you want to do that :P

Inserting a Java string in another string without concatenation?

   String test = String.format("test goes here %s more text", "Testing");

is the closest thing that you could write in Java

VBA string variables does not put constant string value in string concatenating

Try below code :

Sub mySub()

Const strVarA As String = "AA"
Const strVarB As String = "BB"
Const strVarC As String = "CC"

'Dim myStrArray() As String
Dim myString As String
myStrArray = Array("strVarA", "strVarB", "strVarC")

For i = LBound(myStrArray) To UBound(myStrArray)
myURL = "http://www.yahoo.com/news/" & myStrArray(i) & ".html"
'//do something.
Next i

End Sub

Sample Image

Updated after comments

Sub mySub()

Dim myStrArray As Variant
myStrArray = Array("AA", "BB", "CC")

For i = LBound(myStrArray) To UBound(myStrArray)
myurl = "http://www.yahoo.com/news/" & myStrArray(i) & ".html"
MsgBox myurl
Next

End Sub

Include constant in string without concatenating

No.

With Strings, there is no way for PHP to tell string data apart from constant identifiers. This goes for any of the string formats in PHP, including heredoc.

constant() is an alternative way to get hold of a constant, but a function call can't be put into a string without concatenation either.

Manual on constants in PHP



Related Topics



Leave a reply



Submit