Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

To run a non-executable sh script, use:
***
sh myscript
***
To run a non-executable bash script, use:
***
bash myscript
***
To start an executable (which is any file with executable permission); you just specify it by its path:
***
/foo/bar
/bin/bar
./bar
***
To make a script executable, give it the necessary permission:
***
chmod +x bar
./bar
***
When a file is executable, the kernel is responsible for figuring out how to execute it. For non-binaries, this is done by looking at the first line of the file. It should contain a hashbang:
4 years ago
If by all permissions you mean 777

Navigate to folder and
***
chmod -R 777 .
***
4 years ago
Processing the contents of /proc/cpuinfo is needlessly baroque. Use nproc which is part of coreutils, so it should be available on most Linux installs.

Command nproc prints the number of processing units available to the current process, which may be less than the number of online processors.

To find the number of all installed cores/processors use nproc --all

On my 8-core machine:
***
$ nproc --all
8
***
4 years ago
A foreach will give you your index in the form of your $key value, so such a hack shouldn't be necessary.

e.g., in a foreach
***
$index = 0;
foreach($data as $key=>$val) {
// Use $key as an index, or...

// ... manage the index this way..
echo "Index is $index\n";
$index++;
}
***
4 years ago
Here is an example of converting integer to string.
***
$str = (string) $int;
$str = "$int";
***
4 years ago
You can change it via an .htaccess file.

.htaccess files are stored in the same directory as your .php files are. They modify configuration for that folder and all sub-folders. You simply use them by creating an .htaccess file in the directory of your choice (or modify it if present).

The following should enable you to increase your upload limit (if the server provider allows PHP config changes via .htaccess).
***
php_value upload_max_filesize 40M
php_value post_max_size 42M
***
4 years ago
Use this code to get the first and last iteration in each loop
***
foreach($array as $key => $element) {
reset($array);
if ($key === key($array))
echo 'FIRST ELEMENT!';

end($array);
if ($key === key($array))
echo 'LAST ELEMENT!';
}
***
4 years ago
Use this code to check whether an array is empty or not.
***
$playerList = array();
if (!$playerList) {
echo "No players";
} else {
echo "Explode stuff...";
}
// Output is: No players

***
4 years ago
This is done with typecasting:
***
$strvar = (string) $var; // Casts to string
echo $var; // Will cast to string implicitly
var_dump($var); // Will show the true type of the variable
***
In a class you can define what is output by using the magical method __toString. An example is below:

class trees {
public function __toString()
{
return 'Fifty five trees';
}
}

$ex = new trees;
var_dump($ex, (string) $ex);
// Returns: instance of trees and "fifty five trees"
4 years ago
The function json_decode() returns an object by default.

You can access the data like this:
***
var_dump($result->context);
***
If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:
***
var_dump($result->{'from-date'});
***
If you want an array you can do something like this:
***
$result = json_decode($json, true);
***
Or cast the object to an array:
***
$result = (array) json_decode($json);
***
4 years ago
Use *round()* (use if you are expecting a number in float format only, else use number_format()
***
echo round(520.34345, 2); // 520.34
echo round(520.3, 2); // 520.3
echo round(520, 2); // 520
***
4 years ago
You can listen to the 'illuminate.query' event. Before the query add the following event listener:
***
Event::listen('illuminate.query', function($query, $params, $time, $conn)
{
dd(array($query, $params, $time, $conn));
});

DB::table('users')->get();
***
This will print out something like:
***
array(4) {
[0]=>
string(21) "select * from "users""
[1]=>
array(0) {
}
[2]=>
string(4) "0.94"
[3]=>
string(6) "sqlite"
}
***
4 years ago