Application of printf and sprintf

巴扎黑
Release: 2016-11-10 13:51:04
Original
1362 people have browsed it


<?php  
printf(&#39;b: %b <br>c: %c <br>d: %d <bf>f: %f&#39;, 80,80, 80, 80);  
echo &#39;<br />&#39;;  
printf(&#39;%0.2f <br>%+d <br>%0.2f <br>&#39;, 8, 8, 1235.456);  
  
printf(&#39;the cost of %d %s at $%0.2f each is $%0.2f.&#39;, 4, &#39;brooms&#39;, 8.50, (4*8.50));  
  
echo &#39;<br>&#39;;  
$tax = 30;  
printf(&#39;The tax rate is %0.2f%%&#39;, $tax);
Copy after login

Sprint can separate the query and data very well

<!doctype html>  
<html lang="en">  
<head>  
    <meta charset="utf-8">  
    <title>Sorting Multidimensional Arrays</title>  
</head>  
<body>  
<?php  
$db = mysqli_connect(&#39;192.168.31.172&#39;, &#39;root&#39;, &#39;root&#39;, &#39;phpadvanced&#39;);  
mysqli_query($db, "set names utf8");  
  
if(!empty($_POST[&#39;task&#39;])) {  
    $parent_id = 0;  
    if(isset($_POST[&#39;parent_id&#39;]) && filter_var($_POST[&#39;parent_id&#39;], FILTER_VALIDATE_INT, [&#39;min_range&#39; => 1])) {  
        $parent_id = $_POST[&#39;parent_id&#39;];  
    }  
  
    $q = sprintf("INSERT INTO tasks (parent_id, task) VALUES (%d, &#39;%s&#39;)", $parent_id, mysqli_real_escape_string($db, strip_tags($_POST[&#39;task&#39;])));  
    $r = mysqli_query($db, $q);  
  
    if(mysqli_affected_rows($db) == 1) {  
        echo &#39;<p>任务添加成功</p>&#39;;  
    } else {  
        echo &#39;<p>任务添加失败</p>&#39;;  
    }  
}  
?>  
    <form action="add_task.php" method="post">  
        <fieldset>  
            <legend>添加一个任务</legend>  
            <p>任务: <input name="task" type="text" size="60" maxlength="100" required> </p>  
            <p>上级任务: <select name="parent_id">  
                    <option value="0">无上级</option>  
  
                    <?php  
                    $q = &#39;SELECT task_id, parent_id, task FROM tasks WHERE date_completed="0000-00-00 00:00:00" ORDER BY date_added ASC&#39;;  
                    $r = mysqli_query($db, $q);  
                    $tasks = [];  
                    while(list($task_id, $parent_id, $task) = mysqli_fetch_array($r, MYSQLI_NUM)) {  
                        echo "<option value=\"$task_id\">$task</option>";  
                        $tasks[] = [&#39;task_id&#39; => $task_id, &#39;parent_id&#39; => $parent_id, &#39;task&#39; => $task];  
                    }  
                    ?>  
                </select></p>  
            <input name="submit" type="submit" value="添加这个任务" />  
        </fieldset>  
    </form>  
  
<?php  
    // 对任务的父id进行排序  
    function parent_sort($x, $y) {  
        return ($x[&#39;parent_id&#39;] > $y[&#39;parent_id&#39;]);  
    }  
    usort($tasks, &#39;parent_sort&#39;);  
    echo &#39;<h2>当前的任务列表</h2><ul>&#39;;  
    foreach ($tasks as $task) {  
        echo "<li>{$task[&#39;task&#39;]}</li>\n";  
    }  
echo &#39;</ul>&#39;;  
?>  
</body>  
</html>
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!