开发者

what does %s means here? [duplicate]

开发者 https://www.devze.com 2023-02-26 06:11 出处:网络
This question already has answers here: 开发者_如何学JAVAWhat does the syntax '%s' and '%d' mean as shorthand for calling a variable?
This question already has answers here: 开发者_如何学JAVA What does the syntax '%s' and '%d' mean as shorthand for calling a variable? (3 answers) Closed 11 months ago.

What does %s mean?

$sql = "SELECT * 
          FROM page_table 
         WHERE page_name = '%s' 
         LIMIT 1";


It is a formatted string where %s is a placeholder. I suspect that $sql is handed to sprintf to transform it into a real query. Example:

$name = 'posts';
$sql = "SELECT * FROM page_table WHERE page_name = '%s' LIMIT 1";
$formattedSql = sprintf($sql, $name);

This will generate a query looking like:

SELECT * FROM page_table WHERE page_name = 'posts' LIMIT 1

This is very useful when you don't want to fiddle around with quotes and doublequotes.


%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders.

$sql = sprintf($sql, "Test");

This would replace %s with the string "Test". It's also used to make sure that the parameter passed actually fits the placeholder. You might use %d as a placeholder for digits, but if sprintf would receive a string it would complain.


I guess %s is a format symbol for sprintf.

The next line of code may be sth like that:

$sqlquery = sprintf($sql, 'mySearchString');

Real Query would be:

SELECT * FROM page_table WHERE page_name = 'mySearchString' LIMIT 1


we use %s for character code as ASCII 65 is 'A' so till 91 'Z' to understand better you can use for loop starting from 65 to 91 inside this for loop print result using printf

for($i=65; $i<91; $i++){ printf('%s ', chr($i)); }

Above will print

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


$sql = "SELECT * 
          FROM page_table 
         WHERE page_name = '%s' 
         LIMIT 1";
$sql = sprintf($sql, "page");

will give you something like this.

$sql =select * from page_table where page_name='page' LIMIT 1";
0

精彩评论

暂无评论...
验证码 换一张
取 消