sondmk header
การเขียนโปรแกรมด้วยภาษา PHP เบื้้องต้น

PHP : ชนิดของข้อมูล

Post by Goborijung at 2018-12-24 17:04:27 | ID: 75

ข้อมูลจำนวนเต็ม
ค่า 20 ในเลขฐานสิบ: 20
ค่า 020 ในเลขฐานแปด: 16
ค่า 0x20 ในเลขฐานสิบหก: 32

ข้อมูลทศนิยม
ค่า 2.3 เท่ากับ 2.3 
ค่า 2.3e3 เท่ากับ 2300 » ได้มาจาก 2.3x103
ค่า 2e-3 เท่ากับ 0.002 » ได้มาจาก 2x10-3

ข้อมูลแบบ ตรรกะ
ถ้า 5 < 10 จะได้ 1 » True or 1 
ถ้า 5 > 10 จะได้ 0 » False or 0 

ข้อมูลแบบ Array
$arr[0] มีค่าเท่ากับ 0 
$arr[1] มีค่าเท่ากับ 1 
$arr[2] มีค่าเท่ากับ 2 

ตัวแปรแบบ Array
กำหนดให้ $arr = array("1","2","3","4","5"); 
$arr[0] มีค่าเท่ากับ 1 
$arr[1] มีค่าเท่ากับ 2 
$arr[2] มีค่าเท่ากับ 3 
$arr[3] มีค่าเท่ากับ 4 
$arr[4] มีค่าเท่ากับ 5 

ตัวแปร Array แบบ key 
กำหนดให้ $arr = array("user1"=>"1","user2"=>"2","user3"=>"3","user4"=>"4","user5"=>"5"); 
$arr[user1] มีค่าเท่ากับ 1 
$arr[user2] มีค่าเท่ากับ 2 
$arr[user3] มีค่าเท่ากับ 3 
$arr[user4] มีค่าเท่ากับ 4 

PHP : ตัดสตริง , ตัด String , substr , cut string

Post by Goborijung at 2021-01-07 11:58:08 | ID: 944

<?php

$s = "Hello";
echo substr($s,0,2);

//output : He

?>

PHP : ตัวอย่างโค้ดสำหรับ + - วันเวลา (บวกลบวันเวลา) , Date , strtotime

Post by Goborijung at 2019-05-10 17:00:19 | ID: 85

<?=(strtotime($rs['dom_expiration'])-strtotime(date('Y-m-d')))/(24*60*60)?> //วันที่หมดอายุ - วันที่ปัจจุบัน (คืนค่าเป็นจำนวนวัน)

echo date('Y-m-d', strtotime("+2 Month")); //วันที่ปัจจุบัน +2 เดือน
echo date('Y-m-d', strtotime("2020-01-01 -1 Month")); //วันที่ปัจจุบัน -1 เดือน

PHP : นับข้อมูล String ด้วย strlen | (num string) , length

Post by Goborijung at 2020-08-10 09:38:53 | ID: 697

>> PHP
Ex.
echo strlen('This is a book');

// Javascript
<script>
var str = "Hello World!";
var n = str.length;
</script>

PHP : หาจำนวนเฉพาะตั้งแต่ 1 - 100

Post by Goborijung at 2022-02-06 14:04:12 | ID: 1368

<?php

	// หาจำนวนเฉพาะ ตั้งแต่ 1 - 100
 
	$value=100; 
	$i=1; 

	while($i<=$value) 
	{ 
		$c=0; 
		for($j=1;$j<=$i;$j++) 
		{ 
			if($i%$j==0)$c++; 
		}
		
		if($c==2)
		{
			echo($i)."<br>"; 
		}
		
		$i++; 
	}
?>

PHP : หาเลขที่ซ้ำกันใน Array หลายตัว

Post by Goborijung at 2020-07-25 06:58:37 | ID: 687

<?php

$str = "012345678
123456789
234567890
345678901";

echo $str.'<br>';

$str = explode("
",$str);
print_r($str); echo '<br>';

$x = 1;
foreach($str as $val)
{
	${$x} = array();
	for($i=0; $i<strlen(trim($val)); $i++)
	{
		${$x}[] = substr($val,$i,1);
	}
	$x++;
}

$ans = '';
for()
foreach(${1} as $val)
{
	
		if(in_array($val, ${2}) && in_array($val, ${3}) && in_array($val, ${4}))
		{
			$ans .= $val;
		}
	
}

echo $ans.'<br>';

PHP : เรียกใช้ Function ของ PHP ด้วย Javascript

Post by Goborijung at 2020-07-13 14:05:47 | ID: 676

>> HTML
<input type="button" id="sample" value="click" onclick="sam_click()"/>

>> Javascript
<script type="text/javascript">
function sam_click(clicked)
{
 var x="<?php ex(); ?>";//เรียกใช้ฟังก์ชัน ex() ในไฟล์ PHP
 alert(x);
 return false;
}
</script>

>> PHP
<?php
function ex()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("my_db");
$res=mysql_query("call PrcEmployeeDisplay(777)");
$num=mysql_num_rows($res);
echo($num);
}
?>

PHP : แปลง String เป็น Array ด้วย str_split และ explode

Post by Goborijung at 2020-06-23 09:47:24 | ID: 633

>> แปลงด้วย str_split

ตัวอย่างที่ 1.1 ถ้าไม่กำหนดขนาดจะแบ่งเป็น 1 <?php $str = "Hello Share"; $arr1 = str_split($str); print_r($arr1); ?> ตัวอย่างที่ 2.1 แบ่งโดยกำหนดขนาดเป็น 3 <?php $str = "Hello Share"; $arr2 = str_split($str, 3); print_r($arr2); ?> --------------------------------------------------

>> แปลงด้วย explode (ถ้าตัด String ด้วยเครื่องหมายกั้น แนะนำอันนี้จะ)

ตัวอย่างที่ 2.1 delimiter เป็น " " <?php $sentence = "share to your friends"; $pieces = explode(" ", $sentence); print_r($pieces); ?> ตัวอย่างที่ 2.2 delimiter เป็น ',' <?php $input = "share,olanlab"; print_r(explode(',' , $input)); ?> ตัวอย่างที่ 2.3 delimiter เป็น '|' และ limit เป็น 2, -1 <?php $str = 'one|two|three|four'; // positive limit print_r(explode('|', $str, 2)); // negative limit (since PHP 5.1) print_r(explode('|', $str, -1)); ?>

PHP : โปรแกรมคำนวนการผ่อนรถ

Post by Goborijung at 2021-04-21 12:49:09 | ID: 1136

<?php
function calInstallment($carPrice,$percenMoneyDown,$percenInterest,$installment)
{
  // Input
  $carPrice = $carPrice;
  $percenMoneyDown = $percenMoneyDown; 	// % เงินดาวนน์
  $percenInterest = $percenInterest; 	// อัตราดอกเบี้ย % ต่อปี
  $installment = $installment; 		// จำนวนงวด

  // Process
  $moneyDown = $carPrice*($percenMoneyDown/100); //เงินดาวน์
  $moneyTotal = ($carPrice - $moneyDown); //ยอดคงเหลือ(ไม่รวมดอกเบี้ย)
  $InterestPerYear = $moneyTotal*($percenInterest/100); //ดอกเบี่ยต่อปี
  $convInstallmentToYear = $installment/12; //คิดค่างวดเป็นปี
  $InterestTotal = $InterestPerYear*$convInstallmentToYear; //ดอกเบี้ยรวม
  $moneyWithInterest = $moneyTotal+$InterestTotal; //ยอดท้้งหมดรวมดอกเบี่้ย
  $payperinstallment = $moneyWithInterest/$installment; //ยอดชำระในแต่ละงวด(เดือน)
  $moneyTotalPay = $carPrice+$InterestTotal; // ยอดชำระทั้งสิ้น
  $moneyDiff = $moneyTotalPay-$carPrice; // ส่วนต่าง

  // Output
  echo 'ราคารถ : '.number_format($carPrice).'<br>';
  echo 'เงินดาวน์(%) : '.$percenMoneyDown.'%<br>';
  echo 'อัตราดอกเบี้ย(ต่อปี) : '.$percenInterest.'% <br>';
  echo 'จำนวนงวด : '.$installment.' ('.$convInstallmentToYear.'ปี)<br>';
  echo '<hr>';
  echo 'คิดเป็นเงินดาวน์(บาท) : '.number_format($moneyDown).'<br>';
  echo 'ยอดคงเหลือทั้งหมด(ไม่รวมดอกเบี้ย) : '.number_format($moneyTotal).'<br>';
  echo 'ดอกเบี้ย('.$percenInterest.'%) ต่อปี : '.number_format($InterestPerYear).'<br>';
  echo 'ดอกเบี้ย('.$percenInterest.'%) ต่อเดือน : '.number_format($InterestPerYear/12).'<br>';
  echo 'ดอกเบี้ยรวม('.$convInstallmentToYear.'ปี) : '.number_format($InterestTotal).'<br>';
  echo 'ยอดชำระทั้งสิ้น(รวมดอกเบี้ย) : '.number_format($moneyWithInterest).'<br>';
  echo 'ยอดชำระต่องวด(เดือน) : '.number_format($payperinstallment).'<br>';
  echo 'ยอดชำระต่อวัน : '.number_format($payperinstallment/30).'<br>';
  echo 'ยอดชำระทั้งสิ้น : '.number_format($moneyTotalPay).'<br>';
  echo 'ส่วนต่าง : '.number_format($moneyDiff);
}

//calInstallment(ราคารถ,เงินดาวน์(ex 30%),ดอกเบี้ยต่อปี(ex 5%),จำนวนงวด);
calInstallment(539000,5,4.59,84);
?> 

PHP :: หาวันสุดท้ายของเดือนต่างๆ (End Day of Month)

Post by Goborijung at 2020-02-27 14:44:05 | ID: 409

<?php function br(){echo '<br>';}

// หาวันสุดท้ายของเดือนปัจจุบัน
echo date('Y-m-t',strtotime('today')); br();

// หาวันสุดท้ายของเดือนต่อไป
echo date('Y-m-t',strtotime('next month')); br();

// หาวันสุดท้ายของเดือนอื่นๆที่กำนหด
echo date('Y-m-t',strtotime('2014-02-21')); br();

<<<...3456789101112>>>

Framework

Library


เครื่องมือพัฒนาเว็บ



การออกแบบและพัฒนาเว็บไซต์


Download SourceCode



copyAllright © 2016 soundmk.com