How to elegantly splice strings?

Source: Internet
Author: User
{Code...} How can I piece together the word above to make it more elegant?
$username = $this->username ?: 'someone';$email = $this->email ?: Yii::$app->params['adminEmail'];$password = Yii::$app->getSecurity()->generatePasswordHash(    $this->password ?: 'xx');$table = User::tableName();$auth_key = Yii::$app->security->generateRandomString();$status = User::STATUS_ACTIVE;$timestamp = time();$god = 1;$words  = "in the ${table} has a record which contains some value : ";$words .= "'${username}', '${email}', '${password}', '${auth_key}', '${status}','${timestamp}', '${god}', '${timestamp}', '${god}' ";

How can we make the above word more elegant?

Reply content:
$username = $this->username ?: 'someone';$email = $this->email ?: Yii::$app->params['adminEmail'];$password = Yii::$app->getSecurity()->generatePasswordHash(    $this->password ?: 'xx');$table = User::tableName();$auth_key = Yii::$app->security->generateRandomString();$status = User::STATUS_ACTIVE;$timestamp = time();$god = 1;$words  = "in the ${table} has a record which contains some value : ";$words .= "'${username}', '${email}', '${password}', '${auth_key}', '${status}','${timestamp}', '${god}', '${timestamp}', '${god}' ";

How can we make the above word more elegant?

sprintfIs a good solution

But it seems that your code is like debugging all kinds of variables, so there is an artifact.get_defined_vars, Variable names and variable values are worth it

In a word, try to use an ORM or database class to operate the database, instead of manually assembling strings, which can effectively prevent SQL injection. Yii Framework must come with these things, you can take a good look on the document is good: http://www.yiiframework.com/doc/guide/1.1/en/database.dao#binding-para...

If you just concatenate a string, you can directly write the variable name (no curly braces) in double quotation marks. of course, the concatenation of that field is a bit ugly. you can rewrite it like this:

$words = "in the $table has a record which contains some value: ";$words .= implode(", ", array_map(function($item) {    return "'$item'";}, [$username, $email, $password, $auth_key, $status, $timestamp, $god]) );

Save array traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.