Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i'm trying to do a simple condition check like:
$test = 1;
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
echo $test_;
The output is Test-0 but must be Test-1.
Which could be the problem?
Try this : Put brackets for conditions.
$test = 1;
$test_ = ($test==-1)?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
echo $test_;
output :
Test-1
change this
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
to
$test_ = $test==-1?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
working example http://viper-7.com/msuCyk
// output Test-1
Ternary operator in PHP is left to right, therefore, its executed like this:
$test_ = (( ( $test==-1 ? "Test--1" :$test==1) ? "Test-1":$test==0)?"Test-0":"Test");
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I am trying to count the records in the table, but it's showing me nothing in the response, I am working on an API, can anybody help please ?
public function devicesCount()
{
$x = Device::count();
if ($x) {
return ['devices' => $x];
} else {
return ['message' => 'ranii karrezt ydyyyyyyk'];
}
}
use $x = Device::all()->count(); in place of $x = Device::count();
or use Laravel Aggregates like
$count = DB::table('devices')->count();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I execute on my main page some php code which includes :
foreach ($fbdata->feed->data as $fbpost)
{
...
}
How can we convert this , into a loop that goes from (i to z)(0 to 10) ?
Simple for loop
for($i = 0; $i < 10; $i++) {
$fbpost = $fbdata->feed->data[$i];
...
}
or if you like to use the as, try using a foreach but slicing the array before using it
$fbPosts = array_slice($fbdata->feed->data, 0, 10);
foreach($fbPosts as $fbpost) {
...
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I ask a candidate : What do you like ?
His answer is an array [Math,Physics,Chemistry,IT,Sports]
I do have a list :
Software Analyst : [Math,IT,Programing,Stress,Computer]
Truck drive : [Sports,Driving,Traveling,Exchange]
Physic-chemistry teacher :[Math,Physics,Chemistry,Teaching]
What is the best way in PHP to get the jobs with the maximum common tags with the user input?
You use array_intersect() to find the common tags, then compare among the returned tags amount.
$jobs = array(
'software' => array('Math','IT','Programing','Stress','Computer')
'truck' => array('Sports','Driving','Traveling','Exchange')
'physic' => array('Math','Physics','Chemistry','Teaching')
);
$user_tags= array('Math','Physics','Chemistry','IT','Sports');
$max_common = 0;
$desired_job = '';
foreach($jobs as $key=>$job) {
$common = count(array_intersect($job,$user_tags);
if(max_common < $common) {
$desired_job = $key;
$max_common = $common;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to extract the value of the attributes of a html tag that is defined as a php variable:
[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]
values of src and poster should be separated and cast to array
if your text is a php variable, you can use preg_match :
<?php
$text = '[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]';
preg_match('/src=\"([^\"]+)\"/si', $text, $src);
preg_match('/poster=\"([^\"]+)\"/si', $text, $poster);
$array['src'] = $src[1];
$array['poster'] = $poster[1];
print_r($array);
?>
also you can use simplehtmldom
You can use an html parser for this. See this one: http://simplehtmldom.sourceforge.net/.
$html = str_get_html('<video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"></video>');
foreach($html->find('video') as $element)
{
$array['src'] = $element->src;
$array['poster'] = $element->poster;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
must create a rule that captures the first preg_replace bar url:
http://localhost/item/other
must take the rule just 'item' regardless of what comes after
You should use the php parse_url() function
An example would be:
$parts = parse_url("http://localhost/item/other");
$path_parts= explode('/', $parts[path]);
$item = $path_parts[1];
echo $item;
It does not look like you have a specific question. I am assuming you are about write some routes script.
$request_uri = explode('/', $_SERVER['REQUEST_URI']);
$delimiter = array_shift($request_uri);
$controller = array_shift($request_uri);
$action = array_shift($request_uri);
if(preg_match('/item/i', $controller))
{
// do something right
}
else
{
// do something wrong
}