execute php echo as parameter value

php echo variable in html
php echo variable in string
php echo html
php array
php function
echo a function php
how to store echo value in variable php
php echo two variables

I have below code & I am generating tracking_id values manually & its working fine :

<?php

$data = [
"client_reference_id" => "ABCD",
"tracking_id" => "1234",
];
$data = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";

Result :

{"response":[{"tracking_id":"1234",}],

Now i need to create tracking_id dynamically.... so i tried like below :

"tracking_id" => "$r = 'DOCC'. mt_rand(0000000001,9999999999); echo $r;",

I got below Result :

{"response":[{"tracking_id":" = 'DOCC'. mt_rand(0000000001,9999999999); echo ;","

But i should get some random number as tracking_id....

Means php code inside parameter is not working....

As your require 10 digit random number:

function randomNumber($length) {
$result = '';

for($i = 0; $i < $length; $i++) {
    $result .= mt_rand(1, 9);
}

return $result;
}

$data = [
"client_reference_id" => "ABCD",
"tracking_id" => 'DOCC'.randomNumber(10);//no need to echo, just assign it
];
$data = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";

echo - Manual, The major differences to print are that echo accepts an argument list and doesn't have a return value. Parameters ¶. arg1. The parameter to output. The shell_exec () function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix. If the command fails return NULL and the values are not reliable for error checking.

Refactor your code:

$r = 'DOCC'. mt_rand(0000000001,9999999999);

$data = [
  "client_reference_id" => "ABCD",
  "tracking_id" => $r,
];

exec - Manual, If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable. Return Values  echo ( string $arg1 [, string $ ] ) : void. Outputs all parameters. No additional newline is appended. echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function.

Just replace $data array with the following.

$data = [
"client_reference_id" => "ABCD",
"tracking_id" => 'DOCC'.  mt_rand(0000000001,9999999999),
];

How to Display PHP Variable Values, Bound variables pass their value as input and receive the output value, if any, of their associated parameter markers. or an array of input-only parameter values  If you're used to passing command line parameters to a PHP script in a web browser using a query parameter (and then looking it up using $_GET), you may be trying to do something like this Command Line

PHP Echo and Print Statements, PHP supports passing arguments by value (the default), passing by reference, and echo $str; // outputs 'This is a string, and something extra.' ?> Run times: Execute: At a later time, the application binds the values to the parameters, and the database executes the statement. The application may execute the statement as many times as it wants with different values; Compared to executing SQL statements directly, prepared statements have three main advantages:

What is a Parameter in PHP?, This means that if a variable name has parentheses appended to it, PHP will look the same name as whatever the variable evaluates to, and will attempt to execute it. Variable functions won't work with language constructs such as echo, print, User-​defined functions · Function arguments · Returning values · Variable  Accept function as parameter in PHP. Ask Question { // some stuff to execute }); Set a default parameter value for a JavaScript function.

PHP echo Function, The phpResult parameter receives the result of the execution of the PHP function. the PHP developer returned with the echo command (or a similar command). You can pass values of the Alpha, Text, Boolean, Real, Integer, Longint, Date  The values of the $params array are then bound to the. placeholders in the prepared statement when the statement is executed. This is not the same thing as using PDOStatement::bindParam () since this. requires a reference to the variable. PDOStatement::execute () only binds.

Comments
  • Are you just looking to set a specific key in an array? Don't enclose it all in quotes then: "tracking_id" => 'DOCC' . mt_rand(0000000001,9999999999),
  • Possible duplicate of Concatenate PHP function output to a string like variables
  • @h2ooooooo sometimes it work, but sometime it give this message : {"response":[{"tracking_id":"DOCC815327051","status":"REQUEST_REJECTED","message":["trackingId : Invalid tracking ID, Provided value: [DOCC815327051]"]}],
  • Do reduce the max value mt_rand to mt_rand(1111111,9999999)
  • sometimes it work, but sometime it give this message : {"response":[{"tracking_id":"DOCC815327051","status":"REQUEST_REJECTED","message":["trackingId : Invalid tracking ID, Provided value: [DOCC815327051]"]}],
  • check your api, it might only allow digits. replace with "tracking_id" => mt_rand(1111111,9999999);
  • no, it should be in this format only : DOCC-ten digits
  • do this: "tracking_id" => 'DOCC'.mt_rand(11111,99999).mt_rand(11111,99999);
  • Thanks, sometimes it work, but sometime it give this message : {"response":[{"tracking_id":"DOCC815327051","status":"REQUEST_REJECTED","message":["trackingId : Invalid tracking ID, Provided value: [DOCC815327051]"]}],
  • Finally This worked for me : 'DOCC'.mt_rand(11111,99999).mt_rand(11111,99999),
  • can you please tell me how to save that tracking number in database ?
  • It is best to ask a new question, but here's an example stackoverflow.com/questions/18655706/…