17 December, 2020
For loop to While loop Java
Programing Coderfunda
December 17, 2020
Array
No comments
How to find duplicate elements in array in effective way. I mean to say with very less iterations
Programing Coderfunda
December 17, 2020
arrays
No comments
How to find duplicate elements in array in effective way. I mean to say with very less iterations
How to find duplicate elements in array in effective way. I mean to say with very less iterations
Programing Coderfunda
December 17, 2020
arrays
No comments
How to find duplicate elements in array in effective way. I mean to say with very less iterations
How can I create every combination possible for the contents of two arrays?
Programing Coderfunda
December 17, 2020
arrays
No comments
How can I create every combination possible for the contents of two arrays?
How can I create every combination possible for the contents of two arrays?
Programing Coderfunda
December 17, 2020
arrays
No comments
How can I create every combination possible for the contents of two arrays?
16 December, 2020
How to remove empty lines space in Visual Studio Code
Programing Coderfunda
December 16, 2020
No comments
How to remove empty lines in Visual Studio Code
Method 1:
Here are step by step instructions to remove those empty lines.
- Open your code in Visual Studio Code
- From Edit Menu, select Replace or use a short cut key (command + Option + F on Mac or Ctrl + H on Windows)
- In the find box type \n\n
- In the replace box type \n
- Make sure the 'Use Regular Expression' is selected
- Select 'Replace All' button
Method 2:
The above method should remove all the empty line however, if it didn’t then follow these steps:
- Open your code in Visual Studio Code
- From Edit Menu, select Replace or use a short cut key (command + Option + F on Mac or Ctrl + H on Windows)
- In the find box type ^(\s)*$\n
- Leave the replace box empty
- Make sure the 'Use Regular Expression' is selected
- Select 'Replace All' button
If you can't find the above steps, here is a picture.
How to remove empty lines space in Visual Studio Code
Programing Coderfunda
December 16, 2020
No comments
How to remove empty lines in Visual Studio Code
Method 1:
Here are step by step instructions to remove those empty lines.
- Open your code in Visual Studio Code
- From Edit Menu, select Replace or use a short cut key (command + Option + F on Mac or Ctrl + H on Windows)
- In the find box type \n\n
- In the replace box type \n
- Make sure the 'Use Regular Expression' is selected
- Select 'Replace All' button
Method 2:
The above method should remove all the empty line however, if it didn’t then follow these steps:
- Open your code in Visual Studio Code
- From Edit Menu, select Replace or use a short cut key (command + Option + F on Mac or Ctrl + H on Windows)
- In the find box type ^(\s)*$\n
- Leave the replace box empty
- Make sure the 'Use Regular Expression' is selected
- Select 'Replace All' button
If you can't find the above steps, here is a picture.
14 December, 2020
Reference — What does this symbol mean in PHP?
Programing Coderfunda
December 14, 2020
Php Question
No comments
Reference — What does this symbol mean in PHP?
Programing Coderfunda
December 14, 2020
Php Question
No comments
How do you parse and process HTML/XML in PHP?
Programing Coderfunda
December 14, 2020
Php Question
No comments
How do you parse and process HTML/XML in PHP?
Programing Coderfunda
December 14, 2020
Php Question
No comments
StartsWith() and endsWith() functions in PHP
Programing Coderfunda
December 14, 2020
Php Question
No comments
StartsWith() and endsWith() functions in PHP
Programing Coderfunda
December 14, 2020
Php Question
No comments
Deleting an element from an array in PHP
Programing Coderfunda
December 14, 2020
Php Question
No comments
<?php
There are different ways to delete an array element, where some are more useful for some specific tasks than others.
Deleting a single array element
If you want to delete just one array element you can use unset() or alternatively \array_splice().
If you know the value and don’t know the key to delete the element you can use \array_search() to get the key. This only works if the element does not occur more than once, since \array_search returns the first hit only.
unset()
Note that when you use unset() the array keys won’t change. If you want to reindex the keys you can use \array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
// ↑ Key which you want to delete
Output:
[
[0] => a
[2] => c
]
\array_splice() method
If you use \array_splice() the keys will automatically be reindexed, but the associative keys won’t change — as opposed to \array_values(), which will convert all keys to numerical keys.
\array_splice() needs the offset, not the key, as the second parameter.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
\array_splice($array, 1, 1);
// ↑ Offset which you want to delete
Output:
[
[0] => a
[1] => c
]
array_splice(), same as unset(), take the array by reference. You don’t assign the return values of those functions back to the array.
Deleting multiple array elements
If you want to delete multiple array elements and don’t want to call unset() or \array_splice() multiple times you can use the functions \array_diff() or \array_diff_key() depending on whether you know the values or the keys of the elements which you want to delete.
\array_diff() method
If you know the values of the array elements which you want to delete, then you can use \array_diff(). As before with unset() it won’t change the keys of the array.
Code:
$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = \array_diff($array, ["a", "c"]);
// └────────┘
// Array values which you want to delete
Output:
[
[1] => b
]
\array_diff_key() method
If you know the keys of the elements which you want to delete, then you want to use \array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = \array_diff_key($array, [0 => "xy", "2" => "xy"]);
// ↑ ↑
// Array keys which you want to delete
Output:
[
[1] => b
]
If you want to use unset() or \array_splice() to delete multiple elements with the same value you can use \array_keys() to get all the keys for a specific value and then delete all elements.
Deleting an element from an array in PHP
Programing Coderfunda
December 14, 2020
Php Question
No comments
<?php
There are different ways to delete an array element, where some are more useful for some specific tasks than others.
Deleting a single array element
If you want to delete just one array element you can use unset() or alternatively \array_splice().
If you know the value and don’t know the key to delete the element you can use \array_search() to get the key. This only works if the element does not occur more than once, since \array_search returns the first hit only.
unset()
Note that when you use unset() the array keys won’t change. If you want to reindex the keys you can use \array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
// ↑ Key which you want to delete
Output:
[
[0] => a
[2] => c
]
\array_splice() method
If you use \array_splice() the keys will automatically be reindexed, but the associative keys won’t change — as opposed to \array_values(), which will convert all keys to numerical keys.
\array_splice() needs the offset, not the key, as the second parameter.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
\array_splice($array, 1, 1);
// ↑ Offset which you want to delete
Output:
[
[0] => a
[1] => c
]
array_splice(), same as unset(), take the array by reference. You don’t assign the return values of those functions back to the array.
Deleting multiple array elements
If you want to delete multiple array elements and don’t want to call unset() or \array_splice() multiple times you can use the functions \array_diff() or \array_diff_key() depending on whether you know the values or the keys of the elements which you want to delete.
\array_diff() method
If you know the values of the array elements which you want to delete, then you can use \array_diff(). As before with unset() it won’t change the keys of the array.
Code:
$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = \array_diff($array, ["a", "c"]);
// └────────┘
// Array values which you want to delete
Output:
[
[1] => b
]
\array_diff_key() method
If you know the keys of the elements which you want to delete, then you want to use \array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = \array_diff_key($array, [0 => "xy", "2" => "xy"]);
// ↑ ↑
// Array keys which you want to delete
Output:
[
[1] => b
]
If you want to use unset() or \array_splice() to delete multiple elements with the same value you can use \array_keys() to get all the keys for a specific value and then delete all elements.
How can I prevent SQL injection in PHP?
Programing Coderfunda
December 14, 2020
Php Question
No comments
<?php
Use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.
You basically have two options to achieve this
Using PDO (for any supported database driver):
$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');
$stmt->execute([ 'name' => $name ]);
foreach ($stmt as $row) {
// Do something with $row
}
Using MySQLi (for MySQL):
$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name); // 's' specifies the variable type => 'string'
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Do something with $row
}
If you're connecting to a database other than MySQL, there is a driver-specific second option that you can refer to (for example, pg_prepare() and pg_execute() for PostgreSQL). PDO is the universal option.
Correctly setting up the connection
Note that when using PDO to access a MySQL database real prepared statements are not used by default. To fix this you have to disable the emulation of prepared statements. An example of creating a connection using PDO is:
$dbConnection = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'password');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
In the above example the error mode isn't strictly necessary, but it is advised to add it. This way the script will not stop with a Fatal Error when something goes wrong. And it gives the developer the chance to catch any error(s) which are thrown as PDOExceptions.
What is mandatory, however, is the first setAttribute() line, which tells PDO to disable emulated prepared statements and use real prepared statements. This makes sure the statement and the values aren't parsed by PHP before sending it to the MySQL server (giving a possible attacker no chance to inject malicious SQL).
Although you can set the charset in the options of the constructor, it's important to note that 'older' versions of PHP (before 5.3.6) silently ignored the charset parameter in the DSN.
**- Explanation -**
The SQL statement you pass to prepare is parsed and compiled by the database server. By specifying parameters (either a ? or a named parameter like :name in the example above) you tell the database engine where you want to filter on. Then when you call execute, the prepared statement is combined with the parameter values you specify.
The important thing here is that the parameter values are combined with the compiled statement, not an SQL string. SQL injection works by tricking the script into including malicious strings when it creates SQL to send to the database. So by sending the actual SQL separately from the parameters, you limit the risk of ending up with something you didn't intend.
Any parameters you send when using a prepared statement will just be treated as strings (although the database engine may do some optimization so parameters may end up as numbers too, of course). In the example above, if the $name variable contains 'Sarah'; DELETE FROM employees the result would simply be a search for the string "'Sarah'; DELETE FROM employees", and you will not end up with an empty table.
Another benefit of using prepared statements is that if you execute the same statement many times in the same session it will only be parsed and compiled once, giving you some speed gains.
Oh, and since you asked about how to do it for an insert, here's an example (using PDO):
$preparedStatement = $db->prepare('INSERT INTO table (column) VALUES (:column)');
$preparedStatement->execute([ 'column' => $unsafeValue ]);
?>

