Five tips for using PHP to create dynamic web pages

Source: Internet
Author: User
Tags php foreach
1. str_replacestr_replace is a common php function used for string replacement. it is terrible to see some new php users write many lines of str_replace in order to replace a batch of strings. For example, PHP: $ str 'someone's habitat --- w

1. str_replace

Str_replace is a common php function used for string replacement. it is terrible to see some new php users write many lines of str_replace in order to replace a batch of strings.

For example:

PHP:

$ Str = 'someone's habitat --- www.webjx.com ';
$ Str = str_replace ('others', 'badge', $ str );
$ Str = str_replace (', 'di', $ str );
$ Str = str_replace ('habitat ', 'Pig Nest', $ str );
$ Str = str_replace ('www .webjx.com ', 'webjx. com', $ str );

Above, I changed the string four times. actually, I only need to change the writing method, and I will do it in one line:

$ Str = 'someone's habitat --- www.webjx.com ';
$ Str = str_replace (array ('Someone ',', 'habitat ', 'www .webjx.com'), array ('Bad guys ', 'di', 'Pig Nest ', 'webjx. com '), $ str );

2. array

It is often seen that someone writes echo $ arr [some_key] using an array like this.

The above line of code can run, but there is no big title, but if you open the error notice of php. ini, you will receive a large number of errors. The php parser regards "some_key" as a constant, but if some_key is not defined, the parser treats it as a string with tolerance. Therefore, it is better for new students to write a complete picture:

Echo $ arr ['Some _ key']; in this way, there is no title. if you want to put it in double quotation marks, you cannot save the quotation marks. you can write it like this: echo 'The string {$ arr ['Some _ key']} 'in double quotation marks;

3. type tricks

Type tricks are quite easy to use. for example, a variable submitted from a form should normally be an integer type. sometimes it can be used in a lazy way:

$ IntVar = (int) $ _ POST ['post _ var'];

For example, in an array, it is not refreshing to enclose the key value in quotation marks. we can convert it into an object, for example:

$ Arr = array ('name' => 'volcano', '***' => 'male ');
$ Arr = (object) $ arr;
Echo $ arr-> name;
Echo $ arr-> ***; isn't it easy?

4. lambda functions

Lamda functions and array _ * series function applications have miraculous effects. take an example in the php manual:

PHP:

$ Av = array ('the ', 'A', 'That', 'this ');
Array_walk ($ av, create_function ('& $ v, $ k',' $ v = $ v. 'Mango ';'));
Print_r ($ av );
?>

At least one for loop is saved

5. nested loop display of table cells

Nested loops show the cells in the table. this is a very old topic. Usually, you need to add a condition to the back of a cell to determine what to expect. consider whether to output the tr or td tag.

Here we will first introduce a measure. the array_chunk function can be used to compare the neatly output html. for details, see the following example. In this example, we will output a table with four rows and six columns:

$ Arr = range (1, 24); // This will generate an array (1, 2, 3, 4... 24)
$ Arr = array_chunk ($ arr, 6 );
// Output table
?>



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.