Q
Which function is used to write data to a file if the file doesn't exist in PHP?

Answer & Solution

Answer: Option A
Solution:
file_put_contents() is used to write data to a file in PHP. If the file doesn't exist, it creates a new file and writes the data to it. If the file already exists, its contents are overwritten.
Related Questions on Average

What does the 'a' mode do when opening a file in PHP?

A). Appends data to the end of the file

B). Reads data from the file

C). Truncates the file to zero length

D). Creates a new file if it doesn't exist

How do you check if a file is writable in PHP?

A). is_writable()

B). can_write()

C). is_writeable()

D). writable_file()

What happens if you try to open a non-existent file in 'r' mode?

A). fopen() returns false

B). A new file is created

C). The script throws an error

D). The file is opened in read mode

What is the purpose of the 'x' mode when opening a file in PHP?

A). Create a new file and open it for writing

B). Read the file in exclusive mode

C). Execute the file as a PHP script

D). Export file contents to another format

Which function is used to write data to a file in PHP?

A). fwrite()

B). fput()

C). file_write()

D). put_file()

Which mode should be used to open a file for both reading and writing in PHP?

A). r+

B). w+

C). a+

D). rb+

How do you get the file size in PHP?

A). filesize()

B). get_file_size()

C). file_size()

D). size()

Which function is used to read the entire content of a file into a string in PHP?

A). file_get_contents()

B). fread()

C). fget_contents()

D). read_file()

Which mode should be used to open a file for writing in PHP?

A). w

B). r

C). a

D). wb

What is the purpose of the 'r' mode when opening a file in PHP?

A). Read data from a file

B). Write data to a file

C). Append data to a file

D). Truncate a file