Source Code - Simple and useful PHP code snippets for various common tasks (Part 3) | Web Scripts | Crax

Welcome To Crax.Pro Forum!

Check our new Marketplace at Crax.Shop

   Login! SignUp Now!
  • We are in solidarity with our brothers and sisters in Palestine. Free Palestine. To learn more visit this Page

  • Crax.Pro domain has been taken down!

    Alternatives: Craxpro.io | Craxpro.com

Source Code Simple and useful PHP code snippets for various common tasks (Part 3)

Source Code Simple and useful PHP code snippets for various common tasks (Part 3)

LV
1
 

testrest

Member
Joined
Oct 19, 2022
Threads
11
Likes
2
Awards
4
Credits
1,058©
Cash
0$
11. **Working with Cookies**:
```php
setcookie("user", "John", time() + 3600, "/");
```
This code sets a cookie named "user" with the value "John" that will expire in one hour (3600 seconds) and is accessible from the root path ("/"). Cookies are often used to store small pieces of data on the user's browser.

12. **File Handling (Read)**:
```php
$file_content = file_get_contents("filename.txt");
```
This code reads the content of a file named "filename.txt" and stores it in the `$file_content` variable. It's a straightforward way to read the contents of a file in PHP.

13. **File Handling (Write)**:
```php
$file = fopen("newfile.txt", "w");
fwrite($file, "This is some content.");
fclose($file);
```
This code demonstrates how to open a file ("newfile.txt") for writing, write the specified content into it, and then close the file. It's used for creating or overwriting files.

14. **Date and Time**:
```php
echo date("Y-m-d H:i:s");
```
This code uses the `date` function to display the current date and time in the format "YYYY-MM-DD HH:MM:SS". PHP provides powerful date and time manipulation functions.

15. **String Manipulation**:
```php
$str = "Hello, World!";
echo strlen($str); // Length of the string
```
This code initializes a string variable and then uses the `strlen` function to determine and print the length of the string. String manipulation is essential in PHP for tasks like validation and formatting.

These code snippets continue to cover various PHP functionalities, including cookies, file handling, date and time manipulation, and string operations. They can be used as building blocks for a wide range of PHP applications.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Top Bottom